diff --git a/hackerrank.py b/hackerrank.py new file mode 100644 index 0000000..66f8754 --- /dev/null +++ b/hackerrank.py @@ -0,0 +1,26 @@ +# ## Bonus Question 1 +# [HACKERRANK: FIND DIGITS](https://www.hackerrank.com/challenges/find-digits/problem) + +def findDigits(n): + n=str(n) + l=len(n) + summ=0 + for i in range(l): + if int(n[i])==0: + continue + elif int(n)%int(n[i])==0: + summ+=1 + return summ + +# ## Bonus Question 2 +# [HACKERRANK: CAPITALIZE](https://www.hackerrank.com/challenges/capitalize/problem) + +def solve(s): + s=s.split(' ') + for i in range(len(s)): + if s[i]=='': + continue + s[i]=s[i][0].capitalize()+s[i][1:] + s=' '.join([str(item) for item in s]) + return s +solve('chris alan') \ No newline at end of file diff --git a/q1.py b/q1.py new file mode 100644 index 0000000..2e04e1d --- /dev/null +++ b/q1.py @@ -0,0 +1,19 @@ +# ## 1-perfect_number.py +# Perfect number: Perfect number is a positive integer that is equal to the sum of its proper divisors. + +# The smallest perfect number is `6`, which is the sum of `1`, `2`, and `3`. + +# Some other perfect numbers are `28(1+2+4+7+14=28)`, `496` and `8128`. + +# Write a function that finds perfect numbers between `1` and `1000`. +# Check perfect numbers between `1` and `1000` and find the sum of the perfect numbers using reduce and filter functions.
+ +def perfect_number(x): + summ=0 + for i in range(1,x): + if x%i==0: + summ+=i + if summ==x: + return True +from functools import reduce +print(reduce(lambda a,b:a+b,(list(filter(lambda x: perfect_number(x),range(1,1000)))))) \ No newline at end of file diff --git a/q2.py b/q2.py new file mode 100644 index 0000000..03d5066 --- /dev/null +++ b/q2.py @@ -0,0 +1,23 @@ +# ## 2-reading_number.py +# Write a function that outputs the transcription of an input number with two digits. + +# Example: +# ``` +# 28---------------->Twenty Eight + +reading1=['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'] +reading2=['Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'] +reading3=['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen'] +def reading_numb(): + x=input("A number only with two digits: ") + if x.isdigit()==False: + read= x + elif int(x)<10 or int(x)>99: + read= x + elif 20>int(x)>=10: + read='{}--------->{}'.format(x,reading3[int(x[1])]) + + else: + read='{}--------->{} {}'.format(x,reading2[int(x[0])-2],reading1[int(x[1])]) + return read +print(reading_numb()) diff --git a/q3.py b/q3.py new file mode 100644 index 0000000..598b8a7 --- /dev/null +++ b/q3.py @@ -0,0 +1,19 @@ +# ## 3-alphabetical_order.py +# Write a function that takes an input of different words with hyphen (-) in between them and then:
+# * sorts the words in alphabetical order, +# * adds hyphen icon (-) between them, +# * gives the output of the sorted words. + +# Example: +# ``` +# Input >>> green-red-yellow-black-white +# Output >>> black-green-red-white-yellow +# ``` + +def alphabetical_order(): + words=input("Words with - : ") + words=words.split('-') + words.sort() + words='-'.join(words) + return words +print(alphabetical_order()) \ No newline at end of file diff --git a/q4.py b/q4.py new file mode 100644 index 0000000..a511492 --- /dev/null +++ b/q4.py @@ -0,0 +1,18 @@ +# ## 4-unique_list.py +# Write a function that filters all the unique(unrepeated) elements of a given list. + +# Example: +# ``` +# Function call: unique_list([1,2,3,3,3,3,4,5,5]) +# Output : [1, 2, 3, 4, 5] +# ``` + +def unique_list(list): + new_list=[] + for i in list: + if i in new_list: + continue + else: + new_list.append(i) + return new_list +unique_list([1,2,3,3,3,3,4,5,5]) \ No newline at end of file diff --git a/q5.py b/q5.py new file mode 100644 index 0000000..57fdd19 --- /dev/null +++ b/q5.py @@ -0,0 +1,14 @@ +# ## 5-equal_reverse.py +# Write a function that controls the given inputs whether they are equal to their reversed order or not. + +# Example: +# ``` +# Input >>> madam, tacocat, utrecht +# Output >>> True, True, False +# ``` +def reserved(x): + if x==x[::-1]: + return True + else: + return False +print(list(map(lambda x: reserved(x),['madam','tacocat','utrecht']))) \ No newline at end of file