Skip to content

Instantly share code, notes, and snippets.

@orjanv
Created April 6, 2024 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orjanv/d707754ac2bb20fa8760461f61063668 to your computer and use it in GitHub Desktop.
Save orjanv/d707754ac2bb20fa8760461f61063668 to your computer and use it in GitHub Desktop.
Et lite Python program som tar for seg ett og ett navn fra SSB, og sjekker om det gir et palindrom etter omgjøring fra a=1, b=2, c=3, osv.
alfabet = "abcdefghijklmnopqrstuvxyzæøå"
filnavn = "2023-jentenavn.txt"
def is_palindrome(num):
"""Return True if num is a palindrome."""
return str(num) == str(num)[::-1]
def is_prime(num):
"""Return True if num is prime."""
if int(num) < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
with open(filnavn, 'r') as file:
for line in file:
bokstaver = []
for char in line.strip():
bokstav = char.lower()
if bokstav in alfabet:
bokstaver.append(alfabet.index(bokstav) + 1)
bokstavtall = ''.join(map(str, bokstaver))
if is_palindrome(bokstavtall):
print(f'{line.strip().lower()} er et palindromnavn {bokstavtall}')
if is_prime(int(bokstavtall)):
print(f'{line.strip().lower()} er også et primtallnavn {bokstavtall}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment