How can I take an argument from this list so that I can put it in place of a phone?
a10 = ' ' * (2 + length)+ (' ' * length - len(str(phone))).join([str(x) for x in range(1, boardX + 1)])
The idea is that (' ' * length - len(str(phone))) determine the space between numbers depending on whether the number is one digit, two digit, etc.
In the phone place I would like to take an argument from a list to specify the number of its characters. –
example:
boardX = 14
length = 3
output:
target output:
Use a formatting operator rather than calculating spaces yourself. Yo can specify the field width indirectly using *.
a10 = " " * (2 + length) + "".join("%*d" % (length, x) for x in range(1, boardX + 1))
Related
I am trying to parse the coefficient of "a" in a quadratic equation (ax^2 + bx + c).
However, the length of the coefficient can vary.
For instance, here are some of the intended outcomes:
input: 5x^2 + 3x + 4
output: 5
input: 12x^2 + 2x - 6
output: 12
The number could also be in the middle of the equation.
input: 2x - 3x^2 + 4
output: 3
How would I get the whole text before "x^2" until their is a white space?
I have tried using the split() function:
a = equation.split("x^2")
a = a[0]
but this would not work in the third example
Here you are
[-+]?\d+(\.\d+)?(?=x\^)
Test it here
Be aware that this regex will only match simple cases, for example, it will not match 2E3.
Tip: use RegEx 101 to test your pattern:
pattern = re.compile(r'([\+\-\d.]*)x\^2')
tests = [
'5x^2 + 3x + 4',
'12x^2 + 2x - 6',
'2x - 3x^2 + 4',
'x^2 - 2x + 1',
'-x^2 + 6x - 9'
]
for t in tests:
m = pattern.search(t)
text = m.group(1)
if text == '':
coeff = 1
elif text == '-':
coeff = -1
else:
coeff = float(text)
print(coeff)
Prints:
5.0
12.0
3.0
1
-1
I saw that using split is an option too from the question. So you want to get the whole text before the numbers with the ^ operator.
This will only work for these three examples:
x = '5x^2 + 3x + 4'
y = '12x^2 + 2x - 6'
z = '2x - 3x^2 + 4'
print(x.split()[0])
print(y.split()[0])
print(z.split('+')[0])
# split default argument is space
Output
5x^2
12x^2
2x - 3x^2
And If you want to take only the numbers with the ^ operator without the whole text before, change z.split('+')[0] to:
print(z.split()[2])
# 3x^2
I'm trying to make my program return the exact same string but with ** between each character. Here's my code.
def separate(st):
total = " "
n = len(st + st[-1])
for i in range(n):
total = str(total) + str(i) + str("**")
return total
x = separate("12abc3")
print(x)
This should return:
1**2**a**b**c**3**
However, I'm getting 0**1**2**3**4**5**6**.
You can join the characters in the string together with "**" as the separator (this works because strings are basically lists in Python). To get the additional "**" at the end, just concatenate.
Here's an example:
def separate(st):
return "**".join(st) + "**"
Sample:
x = separate("12abc3")
print(x) # "1**2**a**b**c**3**"
A note on your posted code:
The reason you get the output you do is because you loop using for i in range(n): so the iteration variable i will be each index in st. Then when you call str(total) + str(i) + str("**"), you cast i to a string, and i was just each index (from 0 to n-1) in st.
To fix that you could iterate over the characters in st directly, like this:
for c in st:
or use the index i to get the character at each position in st, like this:
for i in range(len(st)):
total = total + st[i] + "**"
welcome to StackOverflow!
I will explain part of your code line by line.
for i in range(n) since you are only providing 1 parameter (which is for the stopping point), this will loop starting from n = 0, 1, 2, ... , n-1
total = str(total) + str(i) + str("**") this add i (which is the current number of iteration - 1) and ** to the current total string. Hence, which it is adding those numbers sequentially to the result.
What you should do instead is total = str(total) + st[i] + str("**") so that it will add each character of st one by one
In addition, you could initialize n as n = len(st)
def parallelogram(a,b):
for i in range(a,0,-1):
for j in range (i,0,-1):
print("*",end='')
for j in range(b,0,-1):
print("+",end='')
for k in range(a-i,0,-1):
print("*",end='')
ı want to write a function which can draw a parallelogram with characters in python.ı couldnt.how can i fix my code?
output must looks like this:
****++++*
***++++**
**++++***
*++++****
With ASCII art you better first analyze the problem. If we want to draw a parallellogram with height a and width w, we first need to think how the first, second, i-th line, etc will look like.
The first line of the prallellogram will contain a asterisks (*), followed by b plusses (+) followed by one asterisk (*). We can write this as:
line1 = '*' * a + '+' * b + '*'
This of course does not solves the entire problem. The second line is almost equal to the first one, except that there is one asterisk less on the left side, and one more on the right side. So:
line2 = '*' * (a-1) + '+' * b + '*' * 2
Every line will remove one asterisk on the left, and add one on the right. So that means that linei has:
linei = '*' * (a+1-i) + '+' * b + '*' * i
Since the parallellogram has a such lines, we can range i from 1 to a+1:
for i in range(1, a+1):
linei = '*' * (a+1-i) + '+' * b + '*' * i
print(linei)
We can simplify this a bit, and write it as:
def parallelogram(a,b):
for i in range(1, a+1):
print('*' * (a+1-i) + '+' * b + '*' * i)
To start with, you are printing with end='', which causes subsequent characters to be printed on the same line. But at some point in your code, you need to add a print() to print on the next line. Start there and see if you figure out where else it needs improvement.
I am trying to create this pattern in python:
*
* *
* * *
* *
*
This is my program so far that I've come up with:
ster = "*"
space = " "
lines = 0
n = 3
x = 1
while lines <= 5:
print space*n, ster*x
n-= 1
x+= 1
lines += 1
What am I doing wrong?
Okay, first of all you can create a list of numbers which represents the number of stars in each line.
number_of_stars = 5
i_list = list(range(number_of_stars))
# extend the list by its inverse i_list[::-1]
# but exclude the first item
i_list.extend(i_list[::-1][1:])
print(i_list) # prints: [0, 1, 2, 3, 4, 3, 2, 1, 0]
Now you can go thru the list and print a multiple of *
for i in i_list:
print('* ' * i)
But this is not aligned properly. By adding multiple empty spaces to the left one can archive a solution:
for i in i_list:
print(' ' * (number_of_stars - i) + '* ' * i)
Note that in python one can repeat a string by using the multiplication symbol:
print('a'*5) # prints: aaaaa
Thank you for the help, I wrote a functional code for the problem. It was supposed to made using while loop(s).
This is what I did:
width = int(input("Width: "))
i = 1
while i < width*2:
if i < width:
print " " * (width-i) + "* " * i
else:
print " " * (i-width) + "* " * (2*width-i)
i = i + 1
Notice you have
3 spaces for 1 star
2 spaces for 2 stars
1 space for 3 stars.
For the upright triangle part of your diamond (including the large part). Then you have
2 spaces for 2 stars
3 spaces for 1 star
Without throwing out the answer, try analysing a certain pattern in what i've just pointed out. It can be achieved with 2 loops ( for or while, depending on your preference).
I am writing a four loop in my program that writes data to a file. I'm wanting for the output to be formatted as follows
frame001 + K.1
frame002 + K.2
...
frame099 + K.99
frame100 + K.100
So far I am doing
for f in range(1, 100):
file.write('frame' + str(f) + ' + K.' + str(f) + '\n')
I have no problem having the K part come out correctly as K.1-K.100, but I don't know how to have prefix zeros/have it output also frame00F to frameFFF with the appropriate amount of preceding zeros.
Using str.format:
>>> 'frame{0:03d} + K.{0}\n'.format(1)
'frame001 + K.1\n'
>>> 'frame{0:03d} + K.{0}\n'.format(100)
'frame100 + K.100\n'
BTW, range(1, 100) will not yield 100. If you want 100 to be included, that should be range(1, 101).
If you are using old version of Python (Python 2.5-), use % operator (String formatting operator) instead (need to specify multiple argument unlike str.format)
>>> 'frame%03d + K.%d\n' % (1, 1)
'frame001 + K.1\n'
>>> 'frame%03d + K.%d\n' % (100, 100)
'frame100 + K.100\n'
If you don't want to repeat arguments, you can pass mapping instead with slightly different format specifier:
>>> 'frame%(i)03d + K.%(i)d\n' % {'i': 1}
'frame001 + K.1\n'