course update of 2023-12-04 14:19:24

This commit is contained in:
Louis Gallet 2023-12-04 14:19:24 +01:00
parent 6c121c2ed3
commit 1bad4bb3c4
Signed by: lgallet
SSH Key Fingerprint: SHA256:qnW7pk4EoMRR0UftZLZQKSMUImbEFsiruLC7jbCHJAY

View File

@ -79,6 +79,7 @@ def ispalind(s):
```python ```python
def dec_to_bin(n, p): def dec_to_bin(n, p):
s = ""
while n > 0: while n > 0:
s = str(n%2) +s s = str(n%2) +s
n = n//2 n = n//2
@ -88,27 +89,14 @@ def dec_to_bin(n, p):
def integer_to_twoscomp(n, p): def integer_to_twoscomp(n, p):
if n < 0: if n < 0:
sign = -2 n = power(2, p) + n
else: return dec_to_bin(n, p)
sign = 1
s = dec_to_bin(n, p) def bin_to_dec(s):
if sign == -1: n = 0
s2 = "" for b in s:
i = len(s) - 1 n = n * 2 + int(b)
while s[i] != "1": return b
s2 = "0" + s2
i -= 1
s2 = "1" + s2
i -= 1
while i >= 0:
if s[i] == "0":
s2 = "1" + s2
else:
s3 = "0" + s3
i -= 1
s = s2
return s
``` ```
2) 2)
@ -127,3 +115,22 @@ def int_to_twoscomp(n, p):
return dec_to_bin(n,p) return dec_to_bin(n,p)
``` ```
## Exercise 2.2 (Frequency)
```python
def frequency(s: string):
count = 0
countInter = 0
elementMax = ""
index = 0
foreach elem in s:
for i in range(index, lenght(s)):
if s[i] == elem:
countInter += 1
if countInter > count:
count = countInter
elementMax = elem
index += 1
return (cout, elementMax)
```