Fun stuff.
This commit is contained in:
parent
2c532a94b5
commit
9b10d747b9
|
@ -322,6 +322,39 @@ else:
|
|||
print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))
|
||||
```
|
||||
|
||||
## More frequency analysis
|
||||
```
|
||||
def more_frequent_item(lst, item1, item2):
|
||||
if (lst.count(item1)) > (lst.count(item2)):
|
||||
return item1
|
||||
if lst.count(item2) > lst.count(item1):
|
||||
return item2
|
||||
if lst.count(item1) == lst.count(item2):
|
||||
return item1
|
||||
|
||||
#Uncomment the line below when your function is done
|
||||
print(more_frequent_item([2, 3, 3, 2, 3, 2, 3, 2, 3], 2, 3))
|
||||
```
|
||||
|
||||
## A creative original solution to grabbing the middle of an index (solving their problem with their names)
|
||||
|
||||
```
|
||||
def middle_element(lst):
|
||||
if len(lst) % 2 > 0:
|
||||
rounded = round(len(lst) / 2)
|
||||
rounded = int(rounded)
|
||||
print(rounded)
|
||||
return lst[rounded]
|
||||
|
||||
elif len(lst) % 2 == 0:
|
||||
position = int((len(lst) / 2) - 1)
|
||||
position2 = int(len(lst) / 2)
|
||||
both = (lst[position] + lst[position2]) / 2
|
||||
return both
|
||||
|
||||
print(middle_element([5, 2, -10, -4, 4, 5, 7]))
|
||||
```
|
||||
|
||||
### Sublists
|
||||
|
||||
Grab a subset of a list using `sublist = letters[1:6]` This would give you index **1-5**.
|
||||
|
|
Loading…
Reference in New Issue