Python打卡Day11

Question 38

Question:

With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line.


Hints:

Use [n1:n2] notation to get a slice from a tuple.


Main Author's Solution: Python 2

1tp = (1,2,3,4,5,6,7,8,9,10)
2tp1 = tp[:5]
3tp2 = tp[5:]
4print tp1
5print tp2

My Solution: Python 3

1tpl = (1,2,3,4,5,6,7,8,9,10)
2
3for i in range(0,5):
4    print(tpl[i],end = ' ')
5print()
6for i in range(5,10):
7    print(tpl[i],end = ' ')

OR

 1tpl = (1,2,3,4,5,6,7,8,9,10)
 2lst1,lst2 = [],[]
 3
 4for i in range(0,5):
 5    lst1.append(tpl[i])
 6
 7for i in range(5,10):
 8    lst2.append(tpl[i])
 9
10print(lst1)
11print(lst2)

OR

1
2'''
3Solution by: CoffeeBrakeInc
4'''
5
6tup = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
7lt = int(len(tup)/2)
8print(tup[:lt], tup[lt:])

OR

 1
 2'''
 3Solution by: AasaiAlangaram
 4'''
 5
 6tp = (1,2,3,4,5,6,7,8,9,10)
 7
 8print('The Original Tuple:',tp)
 9
10[print('Splitted List :{List}'.format(List = tp[x:x+5])) for x in range(0,len(tp),5)]

Question 39

Question:

Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10).


Hints:

Use "for" to iterate the tuple. Use tuple() to generate a tuple from a list.


Main Author's Solution: Python 2

1tp = (1,2,3,4,5,6,7,8,9,10)
2li = list()
3for i in tp:
4	if tp[i]%2 == 0:
5		li.append(tp[i])
6
7tp2 = tuple(li)
8print tp2

My Solution: Python 3

1tpl = (1,2,3,4,5,6,7,8,9,10)
2tpl1 = tuple(i for i in tpl if i%2 == 0)
3print(tpl1)

OR

1tpl = (1,2,3,4,5,6,7,8,9,10)
2tpl1 = tuple(filter(lambda x : x%2==0,tpl))  # Lambda function returns True if found even element.
3                                             # Filter removes data for which function returns False
4print(tpl1)

Question 40

Question:

Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No".


Hints:

Use if statement to judge condition.


Main Author's Solution: Python 2

1s= raw_input()
2if s=="yes" or s=="YES" or s=="Yes":
3    print "Yes"
4else:
5    print "No"

Solution: Python 3

1'''
2Solution by: Seawolf159
3'''
4text = input("Please type something. --> ")
5if text == "yes" or text == "YES" or text == "Yes":
6    print("Yes")
7else:
8    print("No")

1'''
2Solution by: AasaiAlangaram
3'''
4input = input('Enter string:')
5output = ''.join(['Yes' if input == 'yes' or input =='YES' or input =='Yes' else 'No' ])
6print(str(output))

1Solution by: Prashanth
2'''
3x = str(input().lower())
4if x == 'yes':
5    print('Yes')
6else:
7    print('No')

Question 41

Question:

Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10].


Hints:

Use map() to generate a list.Use lambda to define anonymous functions.


Main Author's Solution: Python 2

1li = [1,2,3,4,5,6,7,8,9,10]
2squaredNumbers = map(lambda x: x**2, li)
3print squaredNumbers

My Solution: Python 3

1# No different way of code is written as the requirment is specificly mentioned in problem description
2
3li = [1,2,3,4,5,6,7,8,9,10]
4squaredNumbers = map(lambda x: x**2, li)  # returns map type object data
5print(list(squaredNumbers))               # converting the object into list

Question 42

Question:

Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10].


Hints:

Use map() to generate a list.Use filter() to filter elements of a list.Use lambda to define anonymous functions.


Main Author's Solution: Python 2

1li = [1,2,3,4,5,6,7,8,9,10]
2evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li))
3print evenNumbers

My Solution: Python 3

1def even(x):
2    return x%2==0
3
4def squer(x):
5    return x*x
6
7li = [1,2,3,4,5,6,7,8,9,10]
8li = map(squer,filter(even,li))   # first filters number by even number and the apply map() on the resultant elements
9print(list(li))

Question 43

Question:

Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included).


Hints:

Use filter() to filter elements of a list.Use lambda to define anonymous functions.


Main Author's Solution: Python 2

1evenNumbers = filter(lambda x: x%2==0, range(1,21))
2print evenNumbers

My Solution: Python 3

1def even(x):
2    return x%2==0
3
4evenNumbers = filter(even, range(1,21))
5print(list(evenNumbers))

go to previous day

go to next day

Discussion