Skip to content

Commit 0c91320

Browse files
adrianfuscoeshack94
authored andcommitted
Feature/new python answers (bregman-arie#191)
* New questions and spell check (bregman-arie#181) Added new questions related with KVM, Libvirt and DNF * New answers * Adding a note about which way is faster: Union vs Bitwise * Add comment about slicing vs reversed
1 parent b3934c7 commit 0c91320

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,8 +3031,38 @@ You can then assign a function to a variables like this `x = my_function` or you
30313031
<details>
30323032
<summary>Write a function to determine if a number is a Palindrome</summary><br><b>
30333033

3034+
- Code:
3035+
3036+
```
3037+
from typing import Union
3038+
3039+
def isNumberPalindrome(number: Union[int, str]) -> bool:
3040+
if isinstance(number, int):
3041+
number = str(number)
3042+
return number == number[::-1]
3043+
3044+
print(isNumberPalindrome("12321"))
3045+
```
3046+
3047+
- Using Python3.10 that accepts using bitwise operator '|'.
3048+
3049+
```
3050+
def isNumberPalindrome(number: int | str) -> bool:
3051+
if isinstance(number, int):
3052+
number = str(number)
3053+
return number == number[::-1]
3054+
3055+
print(isNumberPalindrome("12321"))
30343056
```
3057+
3058+
Note: Using slicing to reverse a list could be slower than other options like `reversed` that return an iterator.
3059+
3060+
- Result:
3061+
30353062
```
3063+
True
3064+
```
3065+
30363066
</b></details>
30373067

30383068
#### Python - OOP
@@ -3239,6 +3269,28 @@ False
32393269

32403270
<details>
32413271
<summary>What is the __call__ method?</summary><br><b>
3272+
3273+
It is used to emulate callable objects. It allows a class instance to be called as a function.
3274+
3275+
- Example code:
3276+
3277+
```
3278+
class Foo:
3279+
def __init__(self: object) -> None:
3280+
pass
3281+
def __call__(self: object) -> None:
3282+
print("Called!")
3283+
3284+
f = Foo()
3285+
f()
3286+
```
3287+
3288+
- Result:
3289+
3290+
```
3291+
Called!
3292+
```
3293+
32423294
</b></details>
32433295

32443296
<details>
@@ -3425,6 +3477,24 @@ some_list[:3]
34253477

34263478
<details>
34273479
<summary>How to insert an item to the beginning of a list? What about two items?</summary><br><b>
3480+
3481+
- One item:
3482+
3483+
```
3484+
numbers = [1, 2, 3, 4, 5]
3485+
numbers.insert(0, 0)
3486+
print(numbers)
3487+
```
3488+
3489+
- Multiple items or list:
3490+
3491+
```
3492+
numbers_1 = [2, 3, 4, 5]
3493+
numbers_2 = [0, 1]
3494+
numbers_1 = numbers_2 + numbers_1
3495+
print(numbers_1)
3496+
```
3497+
34283498
</b></details>
34293499

34303500
<details>
@@ -3632,6 +3702,31 @@ list(zip(nums, letters))
36323702

36333703
<details>
36343704
<summary>What is List Comprehension? Is it better than a typical loop? Why? Can you demonstrate how to use it?</summary><br><b>
3705+
3706+
From [Docs](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions): "List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.".
3707+
3708+
It's better because they're compact, faster and have better readability.
3709+
3710+
- For loop:
3711+
3712+
```
3713+
number_lists = [[1, 7, 3, 1], [13, 93, 23, 12], [123, 423, 456, 653, 124]]
3714+
odd_numbers = []
3715+
for number_list in number_lists:
3716+
for number in number_list:
3717+
if number % 2 == 0:
3718+
odd_numbers.append(number)
3719+
print(odd_numbers)
3720+
```
3721+
3722+
- List comprehesion:
3723+
3724+
```
3725+
number_lists = [[1, 7, 3, 1], [13, 93, 23, 12], [123, 423, 456, 653, 124]]
3726+
odd_numbers = [number for number_list in number_lists for number in number_list if number % 2 == 0]
3727+
print(odd_numbers)
3728+
```
3729+
36353730
</b></details>
36363731

36373732
<details>

0 commit comments

Comments
 (0)