### read file contents into a list

Jun 1, 2023CodeCatch

## More Python Posts

```def to_roman_numeral(num):  lookup = [    (1000, 'M'),    (900, 'CM'),    (500, 'D'),    (400, 'CD'),    (100, 'C'),    (90, 'XC'),    (50, 'L'),    (40, 'XL'),    (10, 'X'),    (9, 'IX'),    (5, 'V'),    (4, 'IV'),    (1, 'I'),  ]  res = ''  for (n, roman) in lookup:    (d, num) = divmod(num, n)    res += roman * d  return res
to_roman_numeral(3) # 'III'to_roman_numeral(11) # 'XI'to_roman_numeral(1998) # 'MCMXCVIII'```

#### clamp number

Nov 19, 2022CodeCatch

0 likes • 0 views

`def clamp_number(num, a, b):  return max(min(num, max(a, b)), min(a, b))  clamp_number(2, 3, 5) # 3clamp_number(1, -1, -5) # -1`

#### LeetCode Flood Fill

Oct 15, 2022CodeCatch

0 likes • 0 views

```class Solution(object):    def floodFill(self, image, sr, sc, newColor):        R, C = len(image), len(image[0])        color = image[sr][sc]        if color == newColor: return image        def dfs(r, c):            if image[r][c] == color:                image[r][c] = newColor                if r >= 1: dfs(r-1, c)                if r+1 < R: dfs(r+1, c)                if c >= 1: dfs(r, c-1)                if c+1 < C: dfs(r, c+1)
dfs(sr, sc)        return image```
`print("hellur")`

#### Binary search

Sep 22, 2023AustinLeath

0 likes • 13 views

```# Python binary search functiondef binary_search(arr, target):    left = 0    right = len(arr) - 1    while left <= right:        mid = (left + right) // 2        if arr[mid] == target:            return mid        elif arr[mid] < target:            left = mid + 1        else:            right = mid - 1    return -1
# Usagearr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]target = 7result = binary_search(arr, target)if result != -1:    print(f"Element is present at index {result}")else:    print("Element is not present in array")```
```primes=[]products=[]
def prime(num):    if num > 1:        for i in range(2,num):            if (num % i) == 0:                return False        else:            primes.append(num)            return True
for n in range(30,1000):    if len(primes) >= 20:        break;    else:        prime(n)
for previous, current in zip(primes[::2], primes[1::2]):    products.append(previous * current)    print (products)```