Pattern using nested loop in python 3 - python-3.x

Can anyone help me to print this pattern in python 3.x version:
A
A B
A B C
A B C D
A B C D E
write the code in such a way so as to be able to accept the number of lines from the user.
Kindly explain the steps of ur code.

for i in range(int(input('number: '))):
print(' '.join(chr( ord('A') + k % 26 ) for k in range(0, i + 1)))

Related

python finding the greatest common divisor of two numbers program

I am a new Python learner. I am trying to finding the greatest common divisor of two numbers (a =1071 and b = 462 for example). I have written two programs for this. the first one is working but the second one gives the wrong answer. what is the problem with my program??
# first program (works)
a, b = 1071, 462
while b:
a, b = b, a % b
print(a)
# second program (doesn't work truly)
a = 1071
b = 462
while b:
a = b
b = a % b
print(a)
Explanation:
Yes, HSK is right. In the 2nd loop:
while b:
a = b
b = a % b
print(a)
First a is changed to b and then what you do is b = b% b. But here:
a, b = b, a % b
it is executed as one-line so a is still a.
Solution:
So just add a third variable:
a = 1071
b = 462
while b:
c = a
a = b
b = c % b
print(c)
One thing that distinguishes Python from other programming languages is that it is interpreted rather than compiled. This means that it is executed line by line.
The second doesn't work because, for the calculation of b, you need to use the old a, not the new a that got generated on the line before (this is actually set to b so you will get b % b, which will generally be zero). The equivalent to the first loop would be:
while b:
oldA = a
a = b
b = oldA % b
print(a)
The tuple assignment in Python can be considered an atomic operation where all the values on the right side are loaded up, then assigned to all the variables on the left side.
def divisor(n):
if n ==1 or n ==2 or n ==3:
return []
else:
result=[]
aux=2
while aux <= n:
if n % aux == 0:
result.append(aux)
aux=aux+1
return result
def func (m,n):
div1=divisor(m)
div2=divisor(n)
result =[]
for x in div1:
for y in div2:
if x == y:
result.append(x)
return result
print(func (x,y))

Alternative to conditionals (if)

I'm having difficulties with this task in python 3.7:
"Define a function that given two integers, a and b, returns the value of their sum. However, if the difference of a and b is an even number, the value of the sum is doubled, on the other hand, if the difference is an odd number, the value of the product of a and b gets added to the value of the sum.
For now, do not use conditionals (if)."
I don't know how to do it without using if statements. I would be thankful if anyone could help me.
If a=2 and b=2, the output should be 8.
If a=1 and b=4, the output should be 9.
To determine if the sum of the two integers is even, we use the modulo operator. The following will only evaluate to 0 or 1: (a - b) % 2. 0 = even, 1 = odd.
Knowing this, we can use this value to index into a list which contains 2 functions.
At index 0 we have the double function, which will be called when the value is even, and at index 1 we have the add_product function, called when it's odd.
def process_numbers(a, b):
def double(c):
c *= 2
return c
def add_product(c):
c += a * b
return c
functions = [double, add_product]
c = a + b
c = functions[(a - b) % 2](c)
print(c)
return c
Here is another solution using anonymous (lambda) functions, and taking advantage of the fact that c & 1 will always evaluate to 0 if the difference between a and b is even, and 1 if it's odd.
def process_numbers(a, b):
functions = (lambda: c*2, lambda: c + a*b)
c = a + b
return functions[c & 1]()
And a one-line monstrosity (avert your eyes!):
>>> (lambda a, b: (lambda: (a + b) * 2, lambda: a + b + a * b)[a + b & 1]())(1, 4)
9
>>> (lambda a, b: (lambda: (a + b) * 2, lambda: a + b + a * b)[a + b & 1]())(2, 2)
8
It's a nice little puzzle. Where did you come across it?
Another solution, based off Demi-Lune's functionless solution.
>>> a, b = 2, 2
>>> (a + b & 1)*(a + b + a*b) + (1 - a + b & 1)*(a + b)*2
8
>>> a, b = 1, 4
>>> (a + b & 1)*(a + b + a*b) + (1 - a + b & 1)*(a + b)*2
9
If the sum of a and b is even, the left half evaluates to 0, and the right half evaluates to the sum being doubled.
If the sum of a and b is odd, the right half evaluates to 0, and the left half evaluates to the sum plus the product.

How to code this pattern using the while loop and for loop?

I am supposed to code this pattern:
[1]
[0][0]
[1][1][1]
[0][0][0][0]
[1][1][1][1][1]
[0][0][0][0][0][0]
[1][1][1][1][1][1][1]
[0][0][0][0][0][0][0][0]
[1][1][1][1][1][1][1][1][1]
However, i am only able to give a output like this:
[1]
[0][0]
[1][1][1]
[0][0][0][0]
[1][1][1][1][1]
[0][0][0][0][0][0]
[1][1][1][1][1][1][1]
[0][0][0][0][0][0][0][0]
[1][1][1][1][1][1][1][1][1]
[0][0][0][0][0][0][0][0][0][0]
this is my current code:
a = '[1]'
b = '[0][0]'
for c in range(1,10,2):
print(a)
a = a + '[1][1]'
print(b)
b = b + '[0][0]'
Please help! I am supposed to the pattern with a while loop and for loop. Thanks in advance!
You're printing both a and b in every iteration, so naturally you can't get odd number of lines in your output.
Instead, use a normal range iterator of 9 and alternate between printing and appending to a and b depending on the counter's remainder of 2.
a = '[1]'
b = '[0][0]'
for c in range(9):
if c % 2 == 0:
print(a)
a = a + '[1][1]'
else:
print(b)
b = b + '[0][0]'
This outputs:
[1]
[0][0]
[1][1][1]
[0][0][0][0]
[1][1][1][1][1]
[0][0][0][0][0][0]
[1][1][1][1][1][1][1]
[0][0][0][0][0][0][0][0]
[1][1][1][1][1][1][1][1][1]

Pythagorean triplets using python's list comprehension

I can find out Pythagorean triplets using for loop as follows:
def triplet(n): # Find all the Pythagorean triplets between 1 and n (inclusive)
for a in range(n+1):
for b in range(a):
for c in range(b):
if a*a == b*b + c*c:
print(a, b, c)
I wanted to replace this with a one-liner using list comprehension and tried the following piece:
[a, b, c in range(n+1), range(a), range(b) if a*a == b*b + c*c]
But, I get a syntax error on the closing square bracket. I tried to change the list into tuple using simple brackets, but with no success. May I know how to get it right?
I think you mean
[(a,b,c) for a in range(n+1) for b in range(a) for c in range(b) if a*a == b*b + c*c]
That at least is syntactically valid.
Notice: This solution is only for the problem when a + b + c <= N
Asssume that a<=b<=c, this version is a little faster:
triplet = [(a,b,c) for a in range(1,N//3+1) for b in range(a,N//2+1) for c in range(b,N-1) if a**2 + b**2 == c**2]

unique number of occurrance in a number range

I am interested in figuring out how many times each number occurs once in a numberrange of 5 digits, like 01234, 01243, 01324, etc ....
I know that the occurence is 120 unique times
At the moment I have programmed it with a lot of for loops and a lot of ands.
as you can see in the code
number = 5
for a in range(number):
for b in range(number):
for c in range(number):
for d in range(number):
for e in range(number):
if (a != b and a != c and a != d and a != e and
b != c and b != d and b != e and c != d and
c != e and d != e):
print ('{}{}{}{}{}'.format(a, b, c, d, e))
Is there a different and a nicer way to program the code above?
Greets,
Superfly
Someone suggested using itertools. Here is the code to use:
from itertools import permutations
#generator
factorials = permutations(set(range(5)))
for fact in factorials:
print(fact)
If you would not like to use itertools, but would like to generate every string, you can use a list comprehension to put it all in one line.
rn = range(number)
number= 5
sequences = [(a,b,c,d,e) for a in rn for b in rn for c in rn for d in rn for e in rn if a != b and a!= c and a!= d and a!= e and b!= c and b!=d and b!=e and c!=d and c!= e and d!= e]
Alternatively, you could define a method to recursively create new lists and append items
def rGenFactorials(L, leng=5, collected = [], restrictions=set()):
# base case
if len(L) == leng:
collected.append(L)
return
#induction
options = set(range(leng))
options -= restrictions
for op in options:
# copy restrictions
r = set(restrictions)
# add new restriction
r.add(op)
# copy list
l = L[:]
# append unused int
l.append(op)
# recursive invocation
rGenFactorials(L=l,collected = collected, restrictions=r)
C = []
rGenFactorials([], collected = C)
for c in C:
print(c)

Resources