  # midterm_mnm unknown
python
a year ago
2.5 kB
11
Indexable
Never
```#Cau1a_De1
num1 = [1, 3, 5, 7, 9, 10]
num2 = [2, 4, 6, 8]
num1[-1:] = num2
print(num1)
# [1, 3, 5, 7, 9, 2, 4, 6, 8]

#Cau1b_De1
num = [[1,2,3], [4,5,6], [10,11,12], [7,8,9]]
print(max(num, key=sum))
# [10,11,12]

#Cau1a_De2
num1 = [1, 3, 5, 7, 9, 10]
num2 = [2, 4, 6, 8]
num1[-len(num2)-2:1] = num2
print(num1)
# [2, 4, 6, 8, 3, 5, 7, 9, 10]

#Cau1b_De2
num = [[1,2,3], [4,5,6], [10,11,12], [7,8,9]]
print(min(num, key=sum))
# [1,2,3]

#Cau2_De1
str1 = input()
my_dict = {}
for letter in str1:
my_dict[letter] = my_dict.get(letter, 0) + 1
print(my_dict)
# 'w3resouce'
# {'w': 1, '3': 1, 'r': 2, 'e': 2, 's': 1, 'o': 1, 'u': 1, 'c': 1}

#Cau2_De2
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dic4 = {}
for d in (dic1, dic2, dic3): dic4.update(d)
print(dic4)
# {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

# Cau3
def sort_on_specific_item(lst, n):
result = sorted((lst), key=lambda x: x[n], reverse=False)
return result
# reverse: False will sort ascending, True will sort descending. Default is False
items = [('item2', 10.12), ('item3', 25.10), ('item1', 24.50),('item4', 22.50)]
print("Original list of tuples:")
print(items)
print("\nSort on n element of the tuple of the said list:")
n = 1
print(sort_on_specific_item(items, n))

# Cau4_De1
"""
Factorial
Topic 11: Question 1
A function that calls itself is said to be recursive. The creation of recursive functions is a powerful technique to solve problem that can be broken down into smaller or simpler form. One common use is to find the factorial of a number. The factorial of a number N is simply the number multiplied by the factorial of (N-1). Complete the code given below to calculate and returns the factorial of a numeber.
Examples
>>> factorial(5)
120
>>> factorial(1)
1
>>> factorial(0)
1
"""
x = int(input())
factorial = lambda x: x and x * factorial(x - 1) or 1
print(factorial(x))

# Cau4_De2
"""
Greatest Common Divisor
Topic 11: Question 4
The greatest common divisor (gcd) of 2 or more non-zero integers, is the largest positive integer that divides the numbers without a remainder. Write a function to compute the gcd of 2 integers using Euclid's algorithm:
Examples
>>> gcd(84, 18)
6
>>> gcd(112, 42)
14
>>> gcd(5, 4)
1
"""
a,b = map(int, input().split())
gcd = lambda a,b: a if b ==0 else not a % b and b or gcd(b , a % b)
print(gcd(a,b))
```  