From b98fe35d3e74146233edcbc39c2df069e8d2bea2 Mon Sep 17 00:00:00 2001 From: abregman Date: Thu, 2 Jan 2020 00:17:47 +0200 Subject: [PATCH] Add a couple of answers --- README.md | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b49ce5b..a73bdab 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ :information_source:  This repository contains questions on various DevOps and SRE related topics -:bar_chart:  There are currently **738** questions +:bar_chart:  There are currently **740** questions :books:  To learn more about DevOps check the resources in [DevOpsBit.com](https://devopsbit.com) @@ -2937,6 +2937,23 @@ Last item: some_list[-1] ``` sorted(some_list, reverse=True)[:3] ``` + +Or + +``` +some_list.sort(reverse=True) +some_list[:3] +``` + + +
+Do you know what is the difference between list.sort() and sorted(list)?
+ +* sorted(list) will return a new list (original list doesn't change) +* list.sort() will return None but the list is change in-place + +* sorted() works on any iterable (Dictionaries, Strings, ...) +* list.sort() is faster than sorted(list) in case of Lists
@@ -2950,6 +2967,27 @@ nested_li = [['1', '2', '3'], ['4', '5', '6']]
How to merge two sorted lists into one sorted list?
+ +``` +sorted(li1 + li2) +``` + +Another way: + +``` +i, j = 0 +merged_li = [] + +while i < len(li1) and j < len(li2): + if li1[i] < li2[j]: + merged_li.append(li1[i]) + i += 1 + else: + merged_li.append(li2[j]) + j += 1 + +merged_li = merged_li + merged_li[i:] + merged_li[j:] +```
@@ -3008,6 +3046,12 @@ def my_func(li = []): If we call it 3 times, what would be the result each call?
+ +``` +['hmm'] +['hmm', 'hmm'] +['hmm', 'hmm', 'hmm'] +```
@@ -3021,8 +3065,10 @@ for i in reversed(li): Method 2 ``` -for i in li[::-1]: +n = len(li) - 1 +while n > 0: ... + n -= 1 ```
@@ -3041,14 +3087,26 @@ list(zip(nums, letters))
How to sort a dictionary by values?
+ +``` +{k: v for k, v in sorted(x.items(), key=lambda item: item[1])} +```
How to sort a dictionary by keys?
+ +``` +dict(sorted(some_dictionary.items())) +```
How to merge two dictionaries?
+ +``` +some_dict1.update(some_dict2) +```
##### Common Algorithms Implementation @@ -3458,6 +3516,44 @@ list(zip(range(5), range(50), range(-2))) ```
+#### Misc + + +
+Implement simple calculator for two numbers
+ +``` +def add(num1, num2): + return num1 + num2 + + +def sub(num1, num2): + return num1 - num2 + + +def mul(num1, num2): + return num1*num2 + + +def div(num1, num2): + return num1 / num2 + +operators = { + '+': add, + '-': sub, + '*': mul, + '/': div +} + +if __name__ == '__main__': + operator = str(input("Operator: ")) + num1 = int(input("1st number: ")) + num2 = int(input("2nd number: ")) + print(operators[operator](num1, num2)) +``` +
+ + #### :star: Advanced