-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.py
More file actions
80 lines (66 loc) · 2.26 KB
/
practice.py
File metadata and controls
80 lines (66 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def addTwoNumbers( self, l1, l2): #! <--- We want to pratice this problem and understand why.
head = ListNode()
carry = 0
current = head
while l1 != None or l2 != None or carry != 0:
l1_val = l1.val if l1 else 0 #* <-- Both statements checks if theres a value in linked list, then creates that value
l2_val = l2.val if l2 else 0
total = l1_val + l2_val + carry
current.next = ListNode(total % 10) #* <-- The modulus ensures that there is no decimal for rounding down
carry = total//10 #* <-- Uses floor divison to take the last digit when addeding
l1 = l1.next if l1 else None #* <-- This iterates through the linked list
l2 = l2.next if l2 else None
current = current.next #* <-- Moves through created linked list that stores the values
return head.next
def longestsubstring(s):
seen = {}
length = 0
l = 0
for r in range(len(s)):
char = s[r]
if char in seen and seen[char] >= l:
l = seen[char] + 1
else:
length = max(length, r - l + 1)
seen[char] = r
return length
def findmedian(l1, l2):
new = []
l = 0
for x in l1:
new.append(x)
for k in l2:
new.append(k)
new.sort()
r = len(new) - 1
if len(new) % 2 != 0:
x = ((len(new) - 1)//2)
return float(new[x])
else:
while r != (l+1):
r -= 1
l += 1
median = float((new[l] + new[r])/2.0)
return median
from functools import reduce
def factor(n, k):
new = list(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
new.sort()
strip = []
for x in new:
if x not in strip:
strip.append(x)
strip.sort()
if k > len(strip):
return (-1)
else:
k = k-1
for i in range(len(strip)):
if i == k:
return (strip[i], strip)
print(factor(n=100, k=7))