python3 pattern program diagonal - python-3.x

HOW IT SHOULD LOOK ALIKE
I want pattern to be like this, but I couldn't come up with the loops ((
rows = int(input("number of rows: "))
space = rows - 1
n = 5
for i in range(0, rows):
for j in range(0, space):
print("", end=" " * n)
for j in range(0, i + 1):
print("*", end=" " * n)
space = space - 1
print()
space = 1
for i in range(rows - 1, 0, +1):
for j in range(space, 0):
print(" ", end=" ")
for k in range(0, i - 1):
print("*", end=" " * n)
space = space + 1
print()

rows = int(input("number of rows: "))
width = int(input("width of your window: "))
space = 9 # space between stars
for i in range(rows):
string = " " * (i % space)
for j in range(width):
if j % space == 0:
string += "*"
else:
string += " "
print(string)

Related

fibonacci diamond shape pattern in python

def fib(f, N):
f[1] = 0
f[2] = 1
for i in range(3, N + 1):
f[i] = f[i - 1] + f[i - 2]
def fiboTriangle(n):
N = n * (n + 1) // 2
f = [0]*(N + 1)
fib(f, N)
fiboNum = 1
for i in range(n):
for j in range(n-i):
print(end=" ")
for j in range(i):
print(f[fiboNum], end=" ")
fiboNum+=1
print()
for i in range(n,0,-1):
for j in range(n-i):
print(end=" ")
for j in range(i):
print(f[fiboNum], end=" ")
fiboNum+=1
print()
n = 3
fiboTriangle(n)
this is my code i get this output:
0
1 1
2 3 5
Traceback (most recent call last):
File "/home/ali/Python/emipro.py", line 38, in
fiboTriangle(n)
File "/home/ali/Python/emipro.py", line 34, in fiboTriangle
print(f[fiboNum], end=" ")
IndexError: list index out of range
but i want
diamond shape pattern in output
After receiving your comment I went back to reviewing the type of numbers you were attempting to create in a diamond format. I got a diamond with the numeric sequence you noted by changing:
N = n * (n + 1) // 2
to:
N = n * (n + 1)
Here is the full code.
def fib(f, N):
f[1] = 0
f[2] = 1
for i in range(3, N):
f[i] = f[i - 1] + f[i - 2]
def fiboTriangle(n):
N = n * (n + 1) # Changed this equation
f = [0]*(N + 1)
fib(f, N)
fiboNum = 1
for i in range(n):
for j in range(n-i):
print(end=" ")
for j in range(i):
print(f[fiboNum], end=" ")
fiboNum+=1
print()
for i in range(n,0,-1):
for j in range(n-i):
print(end=" ")
for j in range(i):
print(f[fiboNum], end=" ")
fiboNum+=1
print()
n = 3
fiboTriangle(n)
That produced the following diamond.
craig#Una:~/Python_Programs/Fibonacci$ python3 Fibonacci.py
0
1 1
2 3 5
8 13
21
Give that a try.

I have to write a code for pattern recognition and replacing the pattern?

I have this problem statement where I have a column of patterns that were if the first four bytes have date it should replace those four bytes to ccyy and the rest to N and zeros to Z's
eg. 20190045689 -> CCYYZZNNNNN
if space we need to consider the space as well.
66-7830956 -> NN-NNNZNNN
def patternGeneration(string):
x = re.findall("[\s0-9a-zA-Z]", string)
n = len(x)
j = 0
r = re.compile("\A[^(19|20)]")
y = list(filter(r.match, x))
b = len(y)
for i in range(0, b):
if y[i] == "0":
y[i] = 0
elif y[i] == " ":
y[i] = " "
else:
y[i] = "n"
print(convert(y))
for i in range(0, n):
if x[i] == "0":
x[i] = 0
j = j + 1
elif x[i] == " ":
x[i] = " "
j = j + 1
else:
x[i] = "n"
print(convert(x))
str1 = input("enter the string\t")
patternGeneration(str1)
#convert to new format
def convert(string):
# check for year
head = string[:4]
tail = string[4:]
if head.isnumeric():
if 1900 <= int(head) <= 2099:
head = "CCYY"
new_string = head + tail
return "".join(["Z" if x == "0" else "N" if x.isnumeric() else x for x in str(new_string)])
sample = "20196705540"
print(convert(sample))
#"CCYYNNZNNNZ"
sample = "66-7830956"
print(convert(sample))
#"NN-NNNZNNN"

How do I simplify these nested for loops?

I was given this riddle :
My first idea was to solve it using python. (Later I realized algebra would have been much easier).
Here was my initial code:
a = 0
b = 0
c = 0
d = 0
for a in range(100):
for b in range(100):
for c in range(100):
for d in range(100):
if float(a + b)/4 == 8.0:
if float(a + c)/4 == 13.0:
if float(c - d)/4 == 6.0:
if float(b + d)/4 == 8.0:
print(float(a + b)/4, end=" ")
print(float(a + c)/4, end=" ")
print(float(c + -d)/4, end=" ")
print(float(b + d)/4, end=" and values are:")
print(a/4, end=" ")
print(b/4, end=" ")
print(c/4, end=" ")
print(d/4)
It worked, but obviously this is not very readable.
Looking on stack overflow leads to the solution being recursive functions, however I do not know how exactly to apply this because there are four different mathematical calculations.
For example this is what I tried at first:
val = [0, 0, 0, 0]
def recursive_solution(section):
for val[section] in range(100):
for val[section+1] in range(100):
if section == 0:
if float(val[section])/4 + float(val[section + 1])/4 == 8:
recursive_solution(1)
recursive_solution(0)
But this obviously does not work, as the second index is added to the fourth index, and one of the operations is a subtraction, and you can't add the fourth index to the fifth one because the fifth one doesn't exist.
Here's what I would do from your code: use a cartesian product and and statements:
from collections import product
for a, b, c, d in product(*([range(100)] * 4))
if (
float(a + b) / 4 == 8.0
and float(a + c) / 4 == 13.0
and float(c - d) / 4 == 6.0
and float(b + d) / 4 == 8.0
):
print(float(a + b) / 4, end=" ")
print(float(a + c) / 4, end=" ")
print(float(c + -d) / 4, end=" ")
print(float(b + d) / 4, end=" and values are:")
print(a / 4, end=" ")
print(b / 4, end=" ")
print(c / 4, end=" ")
print(d / 4)

Reason for result of output of Python code

why the while loop is executing more times than expected in printing a pascal triangle?
every time the while loop is executed x is incremented by 1 whereas n remains the same
I just started learning python
please help
memo = {0:1}
def fac(n):
if n not in memo:
memo[n] = n*fac(n-1)
return memo[n]
else:
return memo[n]
def pascal(x, space):
while(x <= n):
for j in range(space):
print(" ", end = "")
for i in range(0, x+1):
print ( int(fac(x)/(fac(i)*fac(x-i))), end = " " )
print("\n", end = "")
x += 1
space -= 1
pascal(x, space)
n = eval(input())
space = n
x = 0
pascal(x, space)
You are using two methods to iterate through the numbers in the pascal function, a while loop, and a recursive call. You just need one of them.
Keeping the while loop:
def pascal(x, space):
while(x <= n):
for j in range(space):
print(" ", end = "")
for i in range(0, x+1):
print ( int(fac(x)/(fac(i)*fac(x-i))), end = " " )
print("\n", end = "")
x += 1
space -= 1
Keeping the recursive call, turning the while into an if:
def pascal(x, space):
if(x <= n):
for j in range(space):
print(" ", end = "")
for i in range(0, x+1):
print ( int(fac(x)/(fac(i)*fac(x-i))), end = " " )
print("\n", end = "")
x += 1
space -= 1
pascal(x, space)
Given 3 as input, both versions print the following:
1
1 1
1 2 1
1 3 3 1

Why aren't the negative numbers being counted properly?

import time
total = 0
pos = 0
zeroes = 0
neg = 0
print('This program will add any seven numbers for you')
time.sleep(2)
print()
a = int(input('Please enter the first number: '))
total = total + a
if a > 0:
pos = pos + 1
elif a == 0:
zeroes = zeroes + 1
elif a < 0:
neg = neg + 1
time.sleep(2)
b = int(input('Please enter the second number: '))
total = total + b
if b > 0:
pos = pos + 1
elif a == 0:
zeroes = zeroes + 1
elif a < 0:
neg = neg + 1
time.sleep(2)
c = int(input('Please enter the third number: '))
total = total + c
if c > 0:
pos = pos + 1
elif c == 0:
zeroes = zeroes + 1
elif c < 0:
neg = neg + 1
time.sleep(2)
d = int(input('Please enter the fourth number: '))
total = total + d
if d > 0:
pos = pos + 1
elif d == 0:
zeroes = zeroes + 1
elif d < 0:
neg = neg + 1
time.sleep(2)
e = int(input('Please enter the fifth number: '))
total =total + e
if e > 0:
pos = pos + 1
elif e == 0:
zeroes = zeroes + 1
elif e < 0:
neg = neg + 1
time.sleep(2)
f = int(input('Please enter the sixth number: '))
total = total + f
if f > 0:
pos = pos + 1
elif f == 0:
zeroes = zeroes + 1
elif f < 0:
neg = neg + 1
time.sleep(2)
g = int(input('Please enter the seventh number: '))
total = total + g
if g > 0:
pos = pos + 1
elif g == 0:
zeroes = zeroes + 1
elif g < 0:
neg = neg + 1
time.sleep(2)
print()
print('The sum of your entries is: ', + total)
time.sleep(2)
print()
print('You entered', + pos, 'positive numbers')
time.sleep(2)
print()
print('You entered', + zeroes, 'zeroes')
time.sleep(2)
print()
print('You entered', + neg, 'negative numbers')
print()
time.sleep(3)
Hello! I have the variable 'neg' keeping a running total of all of the negative numbers that the user enters. It seems as if the negative numbers aren't always being added to the 'neg' running total at the end of the code.
I've been working with Python 3x for about a week now, so be gentle :)
Thanks in advance for the help!
Edit: I have reworked this into a (working) loop per the advice of Kevin, is this a good loop? It seems to work, I'm just looking for pointers as I'm kind of struggling with Python logic. Big thanks to Kevin, wish I could upvote you!
New code posted below:
import time
sums = 0
pos = 0
neg = 0
zero = 0
numb = 0
user_numb = 0
running = True
print('This program will add any 7 numbers for you')
time.sleep(.5)
print()
while running:
user_numb = int(input('Please enter a number: '))
sums = user_numb + sums
numb = numb + 1
print()
if user_numb > 0:
pos = pos + 1
elif user_numb < 0:
neg = neg + 1
elif user_numb == 0:
zero = zero + 1
if numb == 7:
running = False
print()
time.sleep(2)
print('The sum of the numbers entered was: ', + sums)
print()
time.sleep(2)
print('You entered', + pos, 'positive numbers')
print()
time.sleep(2)
print('You entered', + neg, 'negative numbers')
print()
time.sleep(2)
print('You entered', + zero, 'zeroes')
print()
print()
time.sleep(3)
In the section with b you forgot in the copy/pasted code to change all 'a' into 'b' and have still twice an 'a' there:
b = int(input('Please enter the second number: '))
total = total + b
if b > 0:
pos = pos + 1
elif a == 0: # CHANGE TO 'b'
zeroes = zeroes + 1
elif a < 0: # CHANGE TO 'b'
neg = neg + 1
so you see wrong results only in case the second number is a 0 or negative.

Resources