Python get range using start, end characters compatible with Excel [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I make this work with alpha_range(A, ZZ)?
right now it only work until Z
Code:
def alpha_range(start, stop):
""" Returns chars between start char and stop char(A,D -> A,B,C,D).
:param start: start char
:param stop: stop char
:return: list of chars
"""
return [chr(x) for x in range(ord(start), ord(stop)+1)]

You can easily make a bidirectional mapping between A-ZZ and numbers. This actually is pretty similar to a numeric system with different characters to represent the digits.
BASE = ord('Z') - ord('A') + 1
def to_number(str_input):
res = 0
for letter in str_input:
res = res * BASE + ord(letter) - ord('A') + 1
return res
def to_str(int_input):
res = ''
while int_input > 0:
int_input -= 1
res = res + chr(int_input % BASE + ord('A'))
int_input //= BASE
return res[::-1]
Now you can replace ord and chr with this functions.

Related

can you explain how this Loop works and how do we get that output? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
if __name__ == '__main__':
n = int(input())
for i in range (0,n):
result = i**2
print(result)
#input : 5
#output : 0 1 4 9 16
range 0,n = 0,1,2,3,4 and
we gave i**2 but how we got 0,1,4,9,16 as output?
range(start, stop, step)
** = square of Number
start Optional. An integer number specifying at which position to
start. Default is 0
stop Required. An integer number specifying at which position to
stop (not included).
step Optional. An integer number specifying the incrementation. Default is 1
you are passing required parameter as 5 which will not be included in the loop. so as per your calculation it will start from 0
result = 0**2 = 0
result = 1**2 = 1
result = 2**2 = 4
result = 3**2 = 9
result = 4**2 = 16
'i' will not reach 5 because of non-inclusive nature of range() operator.

Extract n first consecutive numbers from a string in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Let's consider this string :
st = 'text1text6253text'
How could I please extract the two first consecutive figures ?
Expected output :
62
You can either use regex with \d{2}and return that, or go over the string:
st = 'text1text6253text'
for i in range(len(st)-1):
if st[i].isdigit() and st[i+1].isdigit():
print(st[i]+st[i+1])
break
import re
def find_con(n, s):
result = re.search('\d{%s}'%n, s)
return result.group(0) if result else result
st = 'text1text6253text'
print(find_con(2, st))
st = 'text1text6253text'
lst = list(st)
lst2 = []
for i,v in enumerate(lst):
if lst[i].isdigit() and lst[i+1].isdigit():
lst2.append(lst[i])
lst2.append(lst[i+1])
ans = int(lst2[0] + lst2[1])
print(ans)
Thanks to your answers, I built a general function that I propose you below :
def extract_n_consecutive_numbers(st,nb):
for i in range(len(st)-nb+1):
is_numeric = True
for j in range(nb):
is_numeric = is_numeric & st[i+j].isdigit()
if is_numeric :
output = ""
for j in range(nb):
output += st[i+j]
return output
return ""
Example :
extract_n_consecutive_numbers('text1text6253text',2)
Out[1]: 62

Print a character 1-15 times [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to find a way to print a number/letter 1 - 15 total times, I'll have to rinse/repeat for the entire alphabet.
aaaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaa
aaaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaa
aaaaaaaaa
aaaaaaaa
aaaaaaa
aaaaaa
aaaaa
aaaa
aaa
aa
a
The most straight up way is to create the string containing 1 - 15 of the same character, and then print it. For this, use the fact that strs can be multiplied by ints like so:
print('a'*15)
Another hint you're probably looking for:
You can use ASCII/Unicode numbers to generate the alphabet like so:
>>> ord('a')
97
>>> ord('z')
122
>>> chr(97)
a
>>> chr(122)
z
so:
>>> print(','.join([chr(_) for _ in range(97,123)]))
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
Note that we make the end of the range 123 because for range(start, stop [,step]): For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.
For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.
So what you can do, putting it together with jmd_dk's answer is something like:
for i in range(97,123):
for j in range(15, 0, -1):
print(chr(i)*j)
Good luck with your homework!
import string
def letters():
alphabet = string.ascii_lowercase
yield from alphabet
for letter in letters():
num = 1
while num <= 15:
print(letter * num)
num += 1

is there possible to resolve this star pyramid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today, i have trying to resolve a small star pyramid :
Input:
5 1
Output:
*
**
***
****
Code:
x = 1
y = 0
m, e = map(int, raw_input().split())
while x < m:
print "\n" * y, "*" * e
m -= 1
e += 1
I did that but there is a better solution?? Thanks =)
I think this can be solved more easily:
stop, first = map(int, raw_input().split())
for i in range(stop - 1):
print '*' * (i + first)
just for fun >:)
class c:
def __init__(s,m,e):
s.e , s.m = sorted([e, m])
s.r = 42
def __iter__(s):
return s
def next(s):
if s.m < s.e:
t = "".join(chr(s.r) for _ in range(s.m))
s.m += 1
return t
else:
raise StopIteration
print "\n".join(c(*map(int,raw_input().split())))
n = int(raw_input())
for i in range(n): print "*"*i
This appears to do what your program intends to do, however I can't quite tell because of the issues I raised in my comment above.

converting a C function into Python3 form? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
New in programming and need to find out what will be the following function in python3?
void expand (char s1 [], char s2[])
{
char c;
int i,j;
i=j=0;
while ((c=s1[i++]) != '\0')
if (s1[i] =='-' && s1[i+1] >=c {
i++;
while (c<s1 [i])
s2 [j++] = c++;
}
else
s2 [j++] =c;
s2 [j] = '\0';
}
The direct translation, working on byte objects only, would be:
def expand(s1):
i = 0
s2 = bytearray()
while i < len(s1):
c = s1[i]
i += 1
if (i + 1) < len(s1) and s1[i] == ord(b'-') and s1[i + 1] >= c:
i += 1
while c < s1[i]:
s2.append(c)
c += 1
else:
s2.append(c)
return bytes(s2)
This appears to expand ranges in the form of a-f into abcdef:
>>> expand(b'a-f')
b'abcdef'
You can use regular expressions to do the same:
import re
_range = re.compile(rb'(.)-(.)')
def _range_expand(match):
start, stop = match.group(1)[0], match.group(2)[0] + 1
if start < stop:
return bytes(range(start, stop))
return match.group(0)
def expand(s1):
return _range.sub(_range_expand, s1)
or, for unicode strings (type str) instead:
import re
_range = re.compile(r'(.)-(.)')
def _range_expand(match):
start, stop = ord(match.group(1)), ord(match.group(2)) + 1
if start < stop:
return ''.join([chr(i) for i in range(start, stop)])
return match.group(0)
def expand(s1):
return _range.sub(_range_expand, s1)

Categories

Resources