Python打卡Day1
Question 1
Question:
Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line.
Hints:
Consider use range(#begin, #end) method.
Main author's Solution: Python 2
1l=[]
2for i in range(2000, 3201):
3 if (i%7==0) and (i%5!=0):
4 l.append(str(i))
5
6print ','.join(l)
My Solution: Python 3
- Using for loops
1for i in range(2000,3201):
2 if i%7 == 0 and i%5!=0:
3 print(i,end=',')
4print("\b")
- Using generators and list comprehension
1print(*(i for i in range(2000, 3201) if i%7 == 0 and i%5 != 0), sep=",")
Question 2
Question:
Write a program which can compute the factorial of a given numbers.The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program: 8 Then, the output should be:40320
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
Main author's Solution: Python 2
1def fact(x):
2 if x == 0:
3 return 1
4 return x * fact(x - 1)
5
6x = int(raw_input())
7print fact(x)
My Solution: Python 3
-
Using While Loop
1n = int(input()) #input() function takes input as string type 2 #int() converts it to integer type 3fact = 1 4i = 1 5while i <= n: 6 fact = fact * i; 7 i = i + 1 8print(fact) -
Using For Loop
1n = int(input()) #input() function takes input as string type 2 #int() converts it to integer type 3fact = 1 4for i in range(1,n+1): 5 fact = fact * i 6print(fact) -
Using Lambda Function
1# Solution by: harshraj22 2 3n = int(input()) 4def shortFact(x): return 1 if x <= 1 else x*shortFact(x-1) 5print(shortFact(n))
1'''Solution by: minnielahoti
2'''
3
4while True:
5try:
6 num = int(input("Enter a number: "))
7 break
8except ValueError as err:
9 print(err)
10
11org = num
12fact = 1
13while num:
14 fact = num * fact
15 num = num - 1
16print(f'the factorial of {org} is {fact}')
1'''Soltuion by: KruthikaSR
2'''
3from functools import reduce
4
5def fun(acc, item):
6 return acc*item
7
8num = int(input())
9print(reduce(fun,range(1, num+1), 1))
Question 3
Question:
With a given integral number n, write a program to generate a dictionary that contains (i, i x i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.Suppose the following input is supplied to the program: 8
Then, the output should be:
1{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.Consider use dict()
Main author's Solution: Python 2
1n = int(raw_input())
2d = dict()
3for i in range(1,n+1):
4 d[i] = i * i
5print d
My Solution: Python 3:
- Using for loop
1n = int(input())
2ans = {}
3for i in range (1,n+1):
4 ans[i] = i * i
5print(ans)
- Using dictionary comprehension
1n = int(input())
2ans={i : i*i for i in range(1,n+1)}
3print(ans)
1'''Solution by: minnielahoti
2 Corrected by: TheNobleKnight
3'''
4
5try:
6 num = int(input("Enter a number: "))
7except ValueError as err:
8 print(err)
9
10dictio = dict()
11for item in range(num+1):
12 if item == 0:
13 continue
14 else:
15 dictio[item] = item * item
16print(dictio)
1'''Solution by: yurbika
2'''
3
4num = int(input("Number: "))
5print(dict(list(enumerate((i * i for i in range(num+1))))))
Conclusion
These was the solved problems of day 1. The above problems are very easy for the basic syntex learners.I have shown some easy ways of coding in my solutions. Lets see how to face and attack new problems in the next day.