Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4f64f4b

Browse filesBrowse files
rootroot
root
authored and
root
committed
work: added ans of some quistions fixed some errors in some problem statements and added one more coding solution
1 parent 879473f commit 4f64f4b
Copy full SHA for 4f64f4b
Expand file treeCollapse file tree

18 files changed

+201
-30
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+129-6Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,22 @@ for i in range(7):
17601760

17611761
## Q. What is a closure in Python?
17621762

1763-
A closure is said to occur when a nested function references a value in its enclosing scope. The whole point here is that it remembers the value.
1763+
In Python, a closure is a function object that has access to variables in its enclosing lexical scope, even when the function is called outside that scope. In other words, a closure allows a function to remember and access the values of the variables in the environment where it was created, even if those variables are no longer in scope when the function is called.
1764+
1765+
Closures are created when a nested function references a value from its enclosing function. The enclosing function returns the nested function, which maintains a reference to the enclosed value. The enclosed value is stored in the closure, which is attached to the nested function object.
1766+
1767+
Here's an example of a closure in Python:
1768+
1769+
```py
1770+
def outer_function(x):
1771+
def inner_function(y):
1772+
return x + y
1773+
return inner_function
1774+
1775+
closure = outer_function(10)
1776+
result = closure(5)
1777+
print(result) # Output: 15
1778+
```
17641779

17651780
```py
17661781
def A(x):
@@ -2434,7 +2449,17 @@ You can create your own function or use one of Python\'s many built-in functions
24342449

24352450
## Q. What is recursion?
24362451

2437-
When a function makes a call to itself, it is termed recursion. But then, in order for it to avoid forming an infinite loop, we must have a base condition. Let\'s take an example.
2452+
Recursion is a programming technique in which a function calls itself to solve a problem. The idea is to break down a complex problem into smaller, simpler problems and solve them in a recursive manner until the base case is reached. The base case is a condition that stops the recursion and returns a value.
2453+
2454+
A common example of a recursive function is the factorial function, which calculates the product of all positive integers up to a given number. Here's an example of a recursive factorial function in Python:
2455+
2456+
```py
2457+
def factorial(n):
2458+
if n == 0:
2459+
return 1
2460+
else:
2461+
return n * factorial(n-1)
2462+
```
24382463

24392464
```py
24402465
def facto(n):
@@ -4309,15 +4334,113 @@ Using bubble sort.
43094334

43104335
## Q. How will you check memory leak on Linux?
43114336

4312-
- valgrind along with gcc.
4337+
There are several tools and techniques that can be used to check for memory leaks on Linux:
4338+
4339+
- Valgrind: Valgrind is a powerful memory debugging tool that can be used to detect memory leaks, among other issues. It works by running the program in a simulated environment that tracks all memory allocations and deallocations, and generates a detailed report of any errors or leaks that it finds.
4340+
4341+
- LeakSanitizer (LSan): LSan is a lightweight tool that is built into the Clang and GCC compilers. It can be used to detect memory leaks at runtime by instrumenting the code with memory tracking code. When a leak is detected, LSan generates a report that includes the stack trace of the leaking allocation.
4342+
4343+
- AddressSanitizer (ASan): ASan is another tool built into the Clang and GCC compilers. It can be used to detect a wide range of memory errors, including memory leaks, buffer overflows, and use-after-free errors. Like LSan, ASan instruments the code with memory tracking code and generates a report when an error is detected.
4344+
4345+
- /proc/meminfo: The /proc/meminfo file contains information about the system's memory usage, including the total amount of memory, the amount of free memory, and the amount of memory used by each process. By monitoring the values in this file over time, it may be possible to detect a memory leak by looking for a steady increase in the amount of memory used by a particular process.
4346+
4347+
- ps and top: The ps and top commands can be used to monitor the system's memory usage and identify processes that are consuming large amounts of memory. By monitoring the memory usage of a particular process over time, it may be possible to detect a memory leak by looking for a steady increase in the amount of memory used by that process.
4348+
4349+
In general, detecting and diagnosing memory leaks can be a complex and time-consuming process. It often requires a combination of tools and techniques, as well as a deep understanding of the code and the system's memory usage patterns.
43134350

43144351
<div align="right">
43154352
<b><a href="#">↥ back to top</a></b>
43164353
</div>
43174354

4318-
#### Q. How can you return multiple values from a function?
4319-
#### Q. What is the fastest way to swap the values bound to two variables?
4320-
#### Q. What is the importance of reference counting?
4355+
## Q. How can you return multiple values from a function?
4356+
4357+
In Python, you can return multiple values from a function by packing them into a tuple or a list. Here's an example using a tuple:
4358+
4359+
```py
4360+
def get_user_info(user_id):
4361+
# ... do some work to retrieve user info ...
4362+
name = "John Doe"
4363+
email = "johndoe@example.com"
4364+
age = 35
4365+
return name, email, age
4366+
4367+
# call the function and unpack the returned values
4368+
user_name, user_email, user_age = get_user_info(123)
4369+
4370+
# print the values
4371+
print(user_name) # Output: John Doe
4372+
print(user_email) # Output: johndoe@example.com
4373+
print(user_age) # Output: 35
4374+
```
4375+
4376+
In this example, the `get_user_info` function returns three values (`name`, `email`, and `age`) as a tuple. When the function is called, the returned tuple is unpacked into individual variables (`user_name`, `user_email`, and `user_age`) using tuple unpacking syntax. The variables can then be used as needed.
4377+
4378+
You can also return multiple values as a list or any other iterable. For example:
4379+
4380+
```py
4381+
def get_user_info(user_id):
4382+
# ... do some work to retrieve user info ...
4383+
name = "John Doe"
4384+
email = "johndoe@example.com"
4385+
age = 35
4386+
return [name, email, age]
4387+
4388+
# call the function and unpack the returned values
4389+
user_info = get_user_info(123)
4390+
user_name, user_email, user_age = user_info
4391+
4392+
# print the values
4393+
print(user_name) # Output: John Doe
4394+
print(user_email) # Output: johndoe@example.com
4395+
print(user_age) # Output: 35
4396+
```
4397+
4398+
In this example, the get_user_info function returns a list instead of a tuple, and the returned values are unpacked into variables using list unpacking syntax.
4399+
4400+
4401+
## Q. What is the fastest way to swap the values bound to two variables?
4402+
4403+
In Python, the fastest way to swap the values bound to two variables is to use tuple unpacking, like this:
4404+
4405+
```py
4406+
a, b = b, a
4407+
```
4408+
4409+
In this code, the values of a and b are swapped by creating a tuple of the values in reverse order ((`b`, `a`)) and unpacking them into the variables a and b. The assignment is performed simultaneously, which makes this method faster than using a temporary variable, which requires an additional assignment.
4410+
4411+
For example:
4412+
4413+
```py
4414+
a = 5
4415+
b = 10
4416+
4417+
# swap the values using tuple unpacking
4418+
a, b = b, a
4419+
4420+
print(a) # Output: 10
4421+
print(b) # Output: 5
4422+
```
4423+
4424+
In this example, the values of `a` and `b` are swapped using tuple unpacking, and the final output shows that the values have indeed been swapped.
4425+
4426+
4427+
## Q. What is the importance of reference counting?
4428+
4429+
In Python, reference counting is a technique used for automatic memory management. It involves keeping track of the number of references to an object and automatically deallocating the object when there are no more references to it.
4430+
4431+
The importance of reference counting in Python can be summarized as follows:
4432+
4433+
- Automatic memory management: Reference counting allows Python to automatically manage memory without requiring explicit memory allocation or deallocation by the programmer. This simplifies programming and reduces the risk of memory leaks and other memory-related bugs.
4434+
4435+
- Efficient memory management: By deallocating objects as soon as they are no longer needed, reference counting helps to minimize the amount of memory used by a program. This is especially important for long-running programs or programs that work with large data sets.
4436+
4437+
- Improved performance: Because Python uses reference counting to manage memory, it can often achieve better performance than other languages that use garbage collection or other memory management techniques. This is because reference counting can be faster and more predictable than other methods, especially for small or short-lived objects.
4438+
4439+
- Object lifetime management: Reference counting ensures that objects are deallocated as soon as they are no longer needed, which helps to avoid problems such as dangling pointers or use-after-free errors. This helps to ensure the correctness and reliability of Python programs.
4440+
4441+
Overall, reference counting is a key feature of Python's automatic memory management system, and it plays an important role in ensuring that Python programs are both efficient and reliable.
4442+
4443+
43214444
#### Q. Do functions return something even if there is not a `return statement?
43224445
#### Q. How do you reverse a list?
43234446
#### Q. How would you merge two sorted lists?

‎programs/add_to_zero.py

Copy file name to clipboardExpand all lines: programs/add_to_zero.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ def add_to_zero(nums):
3535
results = doctest.testmod()
3636

3737
if results.failed == 0:
38-
print "ALL TESTS PASSED!"
38+
print("ALL TESTS PASSED!")

‎programs/anagram_of_palindrome.py

Copy file name to clipboardExpand all lines: programs/anagram_of_palindrome.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ def is_anagram_of_palindrome(word):
4444
results = doctest.testmod()
4545

4646
if results.failed == 0:
47-
print "ALL TESTS PASSED!"
47+
print("ALL TESTS PASSED!")

‎programs/binary_search.py

Copy file name to clipboardExpand all lines: programs/binary_search.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ def binary_search(val):
4646
results = doctest.testmod()
4747

4848
if results.failed == 0:
49-
print "ALL TESTS PASSED"
49+
print("ALL TESTS PASSED")

‎programs/bst_add_child.py

Copy file name to clipboardExpand all lines: programs/bst_add_child.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ def insert(self, new_data):
5757
results = doctest.testmod()
5858

5959
if results.failed == 0:
60-
print "ALL TESTS PASSED!"
60+
print("ALL TESTS PASSED!")

‎programs/calculator.py

Copy file name to clipboardExpand all lines: programs/calculator.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def calculate(inner_expression, operators):
6868
nxt = new_expr[op_index+1]
6969
total = int(prev) * int(nxt)
7070
new_expr = new_expr[:op_index-1] + str(total) + new_expr[op_index+2:]
71-
print new_expr
71+
print(new_expr)
7272

7373
while operators['/'] != []:
7474
op_index = operators['/'].pop()

‎programs/coins.py

Copy file name to clipboardExpand all lines: programs/coins.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ def _coins_2(coins_left, combos, total):
8989
results = doctest.testmod()
9090

9191
if not results.failed:
92-
print 'ALL TESTS PASSED!'
92+
print("ALL TESTS PASSED!")

‎programs/count_employees.py

Copy file name to clipboardExpand all lines: programs/count_employees.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ def count_employees(self):
5555
results = doctest.testmod()
5656

5757
if results.failed == 0:
58-
print "ALL TESTS PASSED"
58+
print("ALL TESTS PASSED!")

‎programs/count_recursively.py

Copy file name to clipboardExpand all lines: programs/count_recursively.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ def count_recursively(lst):
1919
results = doctest.testmod()
2020

2121
if results.failed == 0:
22-
print "ALL TESTS PASSED!"
22+
print("ALL TESTS PASSED!")

‎programs/create_queue.py

Copy file name to clipboardExpand all lines: programs/create_queue.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ def peek(self):
6767
results = doctest.testmod()
6868

6969
if not results.failed:
70-
print "ALL TESTS PASSED!"
70+
print("ALL TESTS PASSED!")
7171

‎programs/create_queue_ll.py

Copy file name to clipboardExpand all lines: programs/create_queue_ll.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,5 @@ def print_queue(self):
171171
results = doctest.testmod()
172172

173173
if not results.failed:
174-
print "ALL TESTS PASSED!"
174+
print("ALL TESTS PASSED!")
175175

‎programs/create_stack.py

Copy file name to clipboardExpand all lines: programs/create_stack.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ def find_min(self):
185185
print
186186
result = doctest.testmod()
187187
if not result.failed:
188-
print "ALL TESTS PASSED. GOOD WORK!"
188+
print("ALL TESTS PASSED. GOOD WORK!")
189189
print
190190

‎programs/decode_string.py

Copy file name to clipboardExpand all lines: programs/decode_string.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ def _decode_2(s, decoded, i):
6969
results = doctest.testmod()
7070

7171
if results.failed == 0:
72-
print 'ALL TESTS PASSED!'
72+
print("ALL TESTS PASSED!")

‎programs/find_mode.py

Copy file name to clipboardExpand all lines: programs/find_mode.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ def find_mode(arr):
4747
results = doctest.testmod()
4848

4949
if not results.failed:
50-
print "ALL TESTS PASSED!"
50+
print("ALL TESTS PASSED!")

‎programs/first_duplicate.py

Copy file name to clipboardExpand all lines: programs/first_duplicate.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,4 @@ def first_duplicate_optimized(arr):
112112
results = doctest.testmod()
113113

114114
if not results.failed:
115-
print "All tests passed!"
115+
print("All tests passed!")

‎programs/highest_product_of_three.py

Copy file name to clipboardExpand all lines: programs/highest_product_of_three.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ def highest_product_of_three(list_of_ints):
6363
results = doctest.testmod()
6464

6565
if results.failed == 0:
66-
print "ALL TESTS PASSED!"
66+
print("ALL TESTS PASSED!")

‎programs/is_unique.py

Copy file name to clipboard
+34-9Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
# import unittest
2+
3+
4+
# def is_unique(string):
5+
# """ Takes a string and returns True if it has all unique characters. """
6+
7+
# str_set = set(string)
8+
9+
# return str_set == string
10+
11+
12+
# class Testing(unittest.TestCase):
13+
# def is_unique_test(self):
14+
# assertEqual(is_unique('asdfghjkl'), True)
15+
# assertEqual(is_unique('1234567asdf'), True)
16+
# assertEqual(is_unique('!@#$%^&asdfg123'), True)
17+
# assertEqual(is_unique('abcdABCD'), True)
18+
19+
# assertEqual(is_unique('asdfghjkll'), False)
20+
# assertEqual(is_unique('1qwerty1'), False)
21+
# assertEqual(is_unique('poiu$asdf$'), False)
22+
23+
# if __name__ == '__main__':
24+
# unittest.main()
25+
126
import unittest
227

328

@@ -10,15 +35,15 @@ def is_unique(string):
1035

1136

1237
class Testing(unittest.TestCase):
13-
def is_unique_test(self):
14-
assertEqual(is_unique('asdfghjkl'), True)
15-
assertEqual(is_unique('1234567asdf'), True)
16-
assertEqual(is_unique('!@#$%^&asdfg123'), True)
17-
assertEqual(is_unique('abcdABCD'), True)
18-
19-
assertEqual(is_unique('asdfghjkll'), False)
20-
assertEqual(is_unique('1qwerty1'), False)
21-
assertEqual(is_unique('poiu$asdf$'), False)
38+
def test_is_unique(self):
39+
self.assertEqual(is_unique('asdfghjkl'), True)
40+
self.assertEqual(is_unique('1234567asdf'), True)
41+
self.assertEqual(is_unique('!@#$%^&asdfg123'), True)
42+
self.assertEqual(is_unique('abcdABCD'), True)
43+
44+
self.assertEqual(is_unique('asdfghjkll'), False)
45+
self.assertEqual(is_unique('1qwerty1'), False)
46+
self.assertEqual(is_unique('poiu$asdf$'), False)
2247

2348
if __name__ == '__main__':
2449
unittest.main()

‎programs/rommon_to_integer.py

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def roman_to_int(s: str) -> int:
2+
roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
3+
result = 0
4+
for i in range(len(s)):
5+
if i < len(s) - 1 and roman_dict[s[i]] < roman_dict[s[i+1]]:
6+
result -= roman_dict[s[i]]
7+
else:
8+
result += roman_dict[s[i]]
9+
return result
10+
11+
12+
'''
13+
>>> roman_to_int('III')
14+
3
15+
>>> roman_to_int('IV')
16+
4
17+
>>> roman_to_int('IX')
18+
9
19+
>>> roman_to_int('LVIII')
20+
58
21+
>>> roman_to_int('MCMXCIV')
22+
1994
23+
'''

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.