Skip to content

Instantly share code, notes, and snippets.

@yuda110
Created July 6, 2016 13:32
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 yuda110/2d73dfd15212d6f4c9a24a26f5064eff to your computer and use it in GitHub Desktop.
Save yuda110/2d73dfd15212d6f4c9a24a26f5064eff to your computer and use it in GitHub Desktop.
import string
text = open('./41-50/words.txt', 'r', encoding='utf-8')
def get_nth_triangle_num(n):
triangle_num = int(0.5*n*(n+1))
return triangle_num
def if_alp_triangle_num(alp_string):
triangle_num, cnt = 0, 1
result_sum = sum([string.ascii_uppercase.index(i)+1 for i in list(alp_string)])
while triangle_num <= result_sum :
triangle_num = get_nth_triangle_num(cnt)
if result_sum == triangle_num :
return True
else :
cnt += 1
return False
def get_num_of_triangle_num_of_words():
result_true = 0
words = text.readline()
for word in words.split(','):
alp_string = word.replace('"', '').replace('"', '')
if if_alp_triangle_num(alp_string) == True:
result_true += 1
return result_true
print(get_num_of_triangle_num_of_words())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment