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.
Related
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))
I am beginning in python programming and I have this exercise that requires me to put text into a box of stars.
Example :
*******
* I *
* am *
* in *
* a *
* box *
*******
I print this :
********
* I *
* am *
* in *
* a *
* box! *
********
So far I have this, I feel like I am close to the solution.
phrase = "I am in a box!"
phraseSplit = phrase.split(' ')
longuestString = max(phraseSplit, key=len)
#print(len(longuestString))
def maxStars(longuestString):
stars = (len(longuestString)+4) * "*"
print(stars)
maxStars(longuestString)
for i in range(len(phraseSplit)):
delta = len(longuestString) - len(phraseSplit[i])
space = int(delta/2 +1)
print("*" + space * " "+ phraseSplit[i] + space * " " +"*")
maxStars(longuestString)
Help me optimize or find other ways that could be more efficient.
Thank you
Here's how I would do it:
phrase = "I am in a box"
words = phrase.split(' ')
longest = max(words, key=len)
width = len(longest)
pretty = "\n".join(["*"*(width+4),*[f"* {w[::-1].center(width)[::-1]} *" for w in words],"*"*(width+4)])
print(pretty)
*******
* I *
* am *
* in *
* a *
* box *
*******
Now let's break it down!
First we do what you did, splitting the phrase into words and extracting other helpful data:
phrase = "I am in a box"
words = phrase.split(' ')
longest = max(words, key=len)
width = len(longest)
Then comes the real algorithm. Let's make a list of one string per line. Clearly the first and last lines are going to be the length of the longest plus 4 (i.e. (wall + padding)*2).
So at the start and end of the list we put:
"*"*(width+4)
Then we need to fill in the actual meat of the box. We're going to use a list iteration to make as many string as there are lines, and join each line with \n:
[line_contents for w in words]
But we need to actually have line_contents be something for each line. So let's replace that with an f-string.
At the start and end of each line there is the padding (ignoring the extra padding the small words have) and an asterisk. So our f-string is currently f"* {padded_word} *".
To actually make the padded words, we can use str.center(width). This gives the whole f-string as f"* {w.center(width)} *".
Now putting everything together we get:
"\n".join(["*"*(width+4),*[f"* {w.center(width)} *" for w in words],"*"*(width+4)]
NOTE: I used the * operator to unpack the list iteration into the larger list.
If we run the program we get:
*******
* I *
* am *
* in *
* a *
* box *
*******
Looks good! But wait one moment - it seems like str.center is making the off-center words go to the right! This is contrary to your example.
However, there's an easy solution: reverse the word, pad it, then reverse it back. This will make the off-center words go left instead. To reverse any indexable object, you can use slice notation: [start, stop, step] --> [beginning, end, -1] --> [::-1].
Now if we add this into what we have so far, we get what I presented initially.
I finally found my own answer :
phrase = "Hi I am in a box!"
phraseSplit = phrase.split(' ')
longuestString = max(phraseSplit, key=len)
#print(len(longuestString))
def maxStars(longuestString):
stars = (len(longuestString)+4) * "*"
print(stars)
maxStars(longuestString)
for i in range(len(phraseSplit)):
delta = len(longuestString) - len(phraseSplit[i])
space = int(delta/2 +1)
if (delta % 2) != 0:
print("*" + space * " "+ phraseSplit[i] + space * " " +" *")
else:
print("*" + space * " "+ phraseSplit[i] + space * " " +"*")
maxStars(longuestString)
I am aware there are optimal ways to do this and I need to figure it out.
I want to be able to print a string and format it into a shape. In the code here it formats into a right triangle, but I wanna do other shapes too. The problem is I can't get the string to truncate at each line and continue on, it simply loops at the first character.
this is what it looks like
hhhhhhhhhhhhhhh
hhhhhhhhhhhhh
hhhhhhhhhhh
hhhhhhhhh
hhhhhhh
hhhhh
hhh
h
but I want it to look like this
hellowor
ldhowar
eyout
oday
?
I've been struggling to wrap my head around this concept a lot lately, I can't seem to loop functions within functions properly. I think I'm probably missing some key part of knowledge for indexes or for loops that's stopping me. But if you could show me here, I might be able to learn a bit more about it. I've tried googling this issue to no avail. I appreciate any help.
Here's my code thus far:
text = ('hello world how are you today?')
def word():
for c in text:
return c
def triangle(i, t = 0):
if i == 0:
return 0
else:
print '' * (t + 1) + word() * (i * 2 - 1)
return triangle (i - 1, t + 1)
triangle (8)
edit:
the other thing I added was this:
def triangle(i, t = 0):
if i == 0:
return 0
else:
for c in text:
print '' * (t + 1) + word() * (i * 2 - 1)
return triangle (i - 1, t + 1)
but it yields the same problem, where by it only prints the first letter from 'text'.
How do I loop through each letter?
Thanks. The basic answer is that you're making this too complicated. Start at the front of the string with your initial row; pass the remainder to a recursive call. Don't bother to take individual characters out of the string: just grab the subset you need.
Note that this has two base cases: Either size hits 0, or you run out of message before that.
def triangle(message, size):
# Size = 0: we're done; ran out of rows
if size == 0:
return
# Not enough message left: print it all
if size >= len(message):
print message
# print "size" characters and go to next line
else:
print message[:size]
triangle(message[size:], size-1)
text = "hello world how are you today?"
triangle(text, 8)
print ""
triangle(text, 7)
Output:
hello wo
rld how
are y
ou to
day?
hello w
orld h
ow ar
e yo
u t
od
a
STRING SLICES
The general form is
str[start : end : step]
This gets you the substring from str[start] through str[end-1], inclusive. If you omit any of the arguments, the defaults are
start = 0
end = -1 (through the end of the string)
step = 1
Since we rarely have regular skips through a string, the step argument is almost always defaulted to 1.
I have to build a hollow diamond like this one:
******
** **
* *
* *
** **
******
Heres what I have so far,
def hollow_diamond(w):
h=int(w/2)
while 0<h:
print('*'*h)
h=h-1
i=1
while i<(w/2+1):
print(i*'*')
i=i+1
However using the code that i have i only get half of the diamond.
***
**
*
*
**
***
Should I be using for loops instead of while to be able to complete the diamond?
You've already figured out how to print the first set of asterisks for each line; good job so far. Now, you need to figure out how many spaces to print. Let's take the first loop, where you're printing h asterisks in a grid of w lines.
You need h asterisks on the left and h more on the right; that's 2*h asterisks total. This leaves s = w - 2*h spaces in the middle.
So, for each line, you need to print ...
h asterisks
s spaces
h more asterisks
Does that move you toward a useful update of your current code?
Building a hollow diamond means, like you said, probably the following:
A line with full asterisks (0 spaces in the middle)
A line with 2 spaces in the middle
A line with 4 spaces in the middle
...
A line with l-2 spaces in the middle
A line with l-2 spaces in the middle
A line with l-4 spaces in the middle
A line with l-6 spaces in the middle
...
A line with full asterisks (l-l spaces in the middle)
n is the "step", or how many asterisks you "lose" in each iteration. l is the size of your square.
So, you algorithm is composed of two parts, the increasing spaces, and the decreasing spaces.
So, your algorithm should be something like this
for (spaces = 0; spaces < size/2 ; spaces = spaces + 1 )
for (asterisk = 0; asterisk < size/2 - spaces; asterisk = asterisk + 1)
print '*'
for (space = 0; space < spaces*2; space = space + 1)
print ' '
for (asterisk = 0; asterisk < size/2 - spaces; asterisk = asterisk + 1)
print '*'
for (spaces = size/2 - 1; spaces >= 0; spaces = spaces - 1)
# The same inner code as above
I purposedly didn't put the python code there, so you can do your homework properly ;), but once you understand the algorithm, that should be pretty easy.
I won't steal from you the joy to fix your homework but this exercise was quite fun so I'll give you another possible version to give you few ideas:
def cool_diamond(w):
r = []
for y in range(w):
s = '*' * (w - y)
r.append("{0}{1}{0}".format(s, ''.join(['-' for x in range(2 * y)]), s))
return '\n'.join(r + r[::-1])
for i in range(3, 6):
print cool_diamond(i)
print('-' * 80)
I'd strongly recommend you take your time first to fix yours! Otherwise you won't learn nothing from the exercise.
Once you've fixed yours you'll feel pretty satisfied for the effort paying off, and then... just then, you can take think whether you can improve YOUR version or refactoring.
Happy coding!
******
**--**
*----*
*----*
**--**
******
--------------------------------------------------------------------------------
********
***--***
**----**
*------*
*------*
**----**
***--***
********
--------------------------------------------------------------------------------
**********
****--****
***----***
**------**
*--------*
*--------*
**------**
***----***
****--****
**********
--------------------------------------------------------------------------------
Here, remember it is only for even numbers entered else the program will execute a number less than that. I have made a code for your requirement.
n=int(input('Enter a number'))
if n%2==0:
pass
else:
n-=1
print("Works only for even number,so decremented the number by 1")
k=n//2
print(a*n)
for i in range(1,k):
print(a*(k-i),end='')
print(b*(c),end='')
print(a*(k-i),end='')
print()
c=c+2
c=c-2
for i in range(1,k):
print(a*(i),end='')
print(b*(c),end='')
print(a*(i),end='')
print()
c-=2
print(a*n)
You can also use it as function. You can easily convert so. You can use both while and for loop.
Check out while loop.
a='*' ; b=' ';c=2
n=int(input('Enter a number'))
if n%2==0:
pass
else:
n-=1
print("Works only for even number,so decremented the number by 1")
k=n//2
print(a*n)
i=1
while i<k:
print(a*(k-i),end='')
print(b*c,end='')
print(a*(k-i),end='')
print()
c=c+2
i+=1
c-=2
i=1
while i<k:
print(a*i,end='')
print(b*c,end='')
print(a*i,end='')
print()
c-=2
i+=1
print(a*n)
Hope it helps.
def muchbetter(x):
count_list = []
for char in "abcdefghijklmnopqrstuvwxyz":
count_list.append(x.lower().count(char))
return tuple(count_list)
def print_stars(x):
tup = muchbetter(x)
stars = [' '*(max(tup) - s) + '*'*s for s in tup if s != 0]
print('\n'.join([''.join(a) for a in list(zip(*stars))]))
so those are two functions, the first one counts how many times a letter appears in a sample text, the second one makes a "tower of asterisk" equal to the number of letters, however, i need the second one to also put all of the letters at the bottom, so what i want it to do is
*
* *
ABCDEFGHIJLMNOPQRSTUVWXYZ
that should be the result if i input "ADD" as X, it would put two asterisk on top of X, 1 asterisk on top of A, and no asterisk ontop of anything else.
You need just to delete if s!= 0 in your list comprehension to make empty space for letters which are not in output of muchbetter.
def muchbetter(x):
count_list = []
for char in "abcdefghijklmnopqrstuvwxyz":
count_list.append(x.lower().count(char))
return tuple(count_list)
def print_stars_order(x):
tup = muchbetter(x)
stars = [' '*(max(tup) - s) + '*'*s for s in tup]
print('\n'.join([''.join(a) for a in list(zip(*stars))]))
print('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
in_str="ADD"
print_stars_order(in_str)
*
* *
ABCDEFGHIJKLMNOPQRSTUVWXYZ