Skip to content

Instantly share code, notes, and snippets.

@ph4r05
Last active November 7, 2018 08:04
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 ph4r05/374f9cda45b07d5e2933c9ee45a64476 to your computer and use it in GitHub Desktop.
Save ph4r05/374f9cda45b07d5e2933c9ee45a64476 to your computer and use it in GitHub Desktop.
# task 1: implement logical formula evaluation
# T, F ~ true, false
# !, A, V, > ~ NOT, AND, OR, IMPLICATION
# Hint: translate atoms to integers (store integers on stack)
# Hint: the infix form: (!(F)) A (!((T) > (F)))
def eval_formula(inp):
stack = []
for token in inp:
pass
eval_formula("F!TF>!A")
# task 2: translate formula to infix notation
def formula_to_infix(inp):
stack = []
for token in inp:
pass
######################################################################################
# DICTIONARIES
sample_text = """
zgegegjhzau wfujqhlagf ak s xgje gx wfujqhlagf lzsl sddgok ugehmlslagf gf uahzwjlwplk, ywfwjslafy sf wfujqhlwv jwkmdl ozauz, ozwf vwujqhlwv, esluzwk lzw jwkmdl gx lzw ghwjslagfk sk ax lzwq zsv twwf hwjxgjewv gf lzw hdsaflwpl. lzw hmjhgkw gx zgegegjhzau wfujqhlagf ak lg sddgo ugehmlslagf gf wfujqhlwv vsls.
zgegegjhzau wfujqhlagf usf tw mkwv lg kwumjwdq uzsaf lgywlzwj vaxxwjwfl kwjnauwk oalzgml wphgkafy kwfkalanw vsls. xgj wpsehdw, kwjnauwk xjge vaxxwjwfl ugehsfawk usf usdumdslw 1) lzw lsp, 2) lzw umjjwfuq wpuzsfyw jslw, sfv 3) kzahhafy gf s ljsfksulagf oalzgml wphgkafy lzw mfwfujqhlwv vsls lg wsuz gx lzgkw kwjnauwk.[1] zgegegjhzau wfujqhlagf usf sdkg tw mkwv lg ujwslw glzwj kwumjw kqklwek kmuz sk kwumjw nglafy kqklwek,[2] ugddakagf-jwkaklsfl zskz xmfulagfk, sfv hjanslw afxgjeslagf jwljawnsd kuzwewk.
zgegegjhzau wfujqhlagf kuzwewk sjw afzwjwfldq esddwstdw. af lwjek gx esddwstadalq, zgegegjhzau wfujqhlagf kuzwewk zsnw owscwj kwumjalq hjghwjlawk lzsf fgf-zgegegjhzau kuzwewk.
ujsay ywfljq,[8] mkafy dsllauw-tskwv ujqhlgyjshzq, vwkujatwv lzw xajkl hdsmkatdw ugfkljmulagf xgj s xmddq zgegegjhzau wfujqhlagf kuzwew. ywfljq'k kuzwew kmhhgjlk tglz svvalagf sfv emdlahdauslagf ghwjslagfk gf uahzwjlwplk, xjge ozauz al ak hgkkatdw lg ugfkljmul uajumalk xgj hwjxgjeafy sjtaljsjq ugehmlslagf.
lzw ugfkljmulagf klsjlk xjge s kgewozsl zgegegjhzau wfujqhlagf kuzwew, ozauz ak daealwv lg wnsdmslafy dgo-vwyjww hgdqfgeasdk gnwj wfujqhlwv vsls. (al ak daealwv twusmkw wsuz uahzwjlwpl ak fgakq af kgew kwfkw, sfv lzak fgakw yjgok sk gfw svvk sfv emdlahdawk uahzwjlwplk, mflad mdlaeslwdq lzw fgakw escwk lzw jwkmdlafy uahzwjlwpl afvwuahzwjstdw.) ywfljq lzwf kzgok zgo lg kdayzldq egvaxq lzak kuzwew lg escw al tgglkljshhstdw, a.w., ushstdw gx wnsdmslafy alk gof vwujqhlagf uajumal sfv lzwf sl dwskl gfw egjw ghwjslagf. xafsddq, zw kzgok lzsl sfq tgglkljshhstdw kgewozsl zgegegjhzau wfujqhlagf kuzwew usf tw ugfnwjlwv aflg s xmddq zgegegjhzau wfujqhlagf lzjgmyz s jwumjkanw kwdx-wetwvvafy. xgj ywfljq'k "fgakq" kuzwew, lzw tgglkljshhafy hjguwvmjw wxxwulanwdq "jwxjwkzwk" lzw uahzwjlwpl tq shhdqafy lg al lzw vwujqhlagf hjguwvmjw zgegegjhzausddq, lzwjwtq gtlsafafy s fwo uahzwjlwpl lzsl wfujqhlk lzw ksew nsdmw sk twxgjw tml zsk dgowj fgakw. tq "jwxjwkzafy" lzw uahzwjlwpl hwjagvausddq ozwfwnwj lzw fgakw yjgok lgg dsjyw, al ak hgkkatdw lg ugehmlw sjtaljsjq fmetwj gx svvalagfk sfv emdlahdauslagfk oalzgml afujwskafy lzw fgakw lgg emuz. ywfljq tskwv lzw kwumjalq gx zak kuzwew gf lzw skkmewv zsjvfwkk gx log hjgtdwek: uwjlsaf ogjkl-uskw hjgtdwek gnwj avwsd dsllauwk, sfv lzw khsjkw (gj dgo-owayzl) kmtkwl kme hjgtdwe. ywfljq'k hz.v. lzwkak[9] hjgnavwk svvalagfsd vwlsadk.
"""
def get_alphabet():
return [chr(ord('a') + x) for x in range(26)]
def shift_text(inp, shift):
alp = get_alphabet()
res = []
inp = inp.lower()
for x in inp:
if x not in alp:
res.append(x)
else:
res.append(chr(ord('a') + ((ord(x) - ord('a') + shift) % 26)))
return res
# TASK 3:
# 1. Implement frequency analysis of characters (i.e., count occurrences of characters in the given string)
# 2. Print the frequency table, sorted lexicographically by the occurrences (most first) and the character (if same occurrence => go by alphabet)
# 3. Find frequency table for common english text on the internet, find the most common letter in english texts. Compute the shift from this letter and the letter you computed
# 4. Decode Caesar-cipher encrypted sample text.
# 5. Frequency analysis of words. Print 10 most frequent long (len > 4) words in a) alphabetical order, b) lexicographically by the key occurrences, count
def frequency_analysis(inp):
freq = {}
for ch in inp:
pass
#################################################################################
# Sorting
people = [
{'name': 'John', 'age': 21, 'hair': 'brown', 'salary': 8000},
{'name': 'Maria', 'age': 40, 'hair': 'blond', 'salary': 9000},
{'name': 'Stephen', 'age': 35, 'hair': 'black', 'salary': 1000},
{'name': 'Samara', 'age': 35, 'hair': 'brown', 'salary': 6100},
{'name': 'Amadeus', 'age': 21, 'hair': 'wicked', 'salary': 5500},
{'name': 'John', 'age': 23, 'hair': 'none', 'salary': 7000},
{'name': 'Alexandra', 'age': 21, 'hair': 'black', 'salary': 8600},
{'name': 'Sofia', 'age': 40, 'hair': 'black', 'salary': 9000},
{'name': 'Samuel', 'age': 35, 'hair': 'black', 'salary': 6100},
{'name': 'Harry', 'age': 23, 'hair': 'brown', 'salary': 8000},
]
# Task:
# - print in the format: "NAME_AGE_SALARY_HAIR" by the order: salary, age, name
# User sorted() and lambda function for sorting
#################################################################################
# 2Ds
#
# Task 1: generate null matrix N x M
# Task 2: print given matrix
# Task 3: initialize null matrix so it has 1 on all edges and both diagonals (big check-box)
# Task 4: compute sum of all columns of the following matrix, find index of column with the maximal sum
matrix = [
[ 32, -84, -29, -77, 53, 43, -9, -67, -92, 30, 95, 16],
[ 27, 46, -57, 12, 10, 52, -87, -94, 23, 72, -13, -2],
[ 42, -10, -76, 58, 26, 86, 87, -28, -97, 84, -16, -88],
[-86, -54, -50, -27, 71, -59, -57, -37, 38, 18, -61, 80],
[-62, 52, 88, -7, 58, 7, 48, -79, 38, -97, -18, -81],
[ 29, 95, 21, -46, -26, 47, -88, -48, -67, 65, -85, -67],
[ 75, 39, 64, 31, -74, -33, 62, -5, 53, 26, -47, 53],
[ -2, 81, 97, -3, 13, -43, -41, -51, 77, -64, -97, 64],
[-35, -47, -82, -81, -62, 9, 65, -82, 7, -30, -90, 44],
[ 86, -48, 62, 89, -4, -64, 22, -72, -50, 14, -15, 21],
[-97, 17, 88, -33, 48, -3, -93, 87, 41, 96, -81, 10],
[ 8, 37, -65, -44, -15, -24, -94, -46, -42, 19, 30, 20],
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment