-
Notifications
You must be signed in to change notification settings - Fork 12
Solutions-Class5-Python-Week3 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| """This function 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.""" | ||
|
|
||
|
|
||
| def sorter(): | ||
| elements=input("Enter words which you want to sort(split with hyphen '-': ").split('-') | ||
| #inputs will be in elements list,afterwards we use sort() for sorting list | ||
| elements.sort() | ||
| return('-'.join(elements)) #join() method separates with '-' | ||
| print(sorter()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
|
|
||
| # We check given list elements whether palindrome(equal its reversed order) | ||
|
|
||
|
|
||
|
|
||
| def palindrome_chck(): | ||
| # Initially we ask for enter list where elements splitted by coma | ||
| lis=input("Check list elements whether palindrome(split with comma ',') :").split(',') | ||
| control=[] #We check every each words individually by "For Loop",afterwards add results in this list. | ||
| for i in lis: | ||
| if i==i[::-1]: | ||
| control.append(True) | ||
| else: | ||
| control.append(False) | ||
|
|
||
| return(control) #Results will be shown here as same order with given list due to list.append | ||
| print(palindrome_chck()) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clear |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """This func ensures that the first and last names of people begin with a | ||
| capital letter in their passports. For example, | ||
| alison heck should be capitalised correctly as Alison Heck.""" | ||
|
|
||
| #to capitalize the given full name appropriately. | ||
| # capitalize() method converts first letter of words to a capital letter | ||
| def name_check(): | ||
| n_list=input("Write your full name splitted with space ' ') : ").split(' ') | ||
| name=(n_list[0]).capitalize() | ||
| surname=(n_list[1]).capitalize() | ||
| fullname=[name,surname] | ||
| Cap_f_n_=(" ".join(fullname)) | ||
| return(Cap_f_n_) | ||
|
|
||
| print(name_check()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clear |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| def name_check(): | ||
| n_list=input("Write your full name splitted with space ' ') : ").split(' ') | ||
| cname=[] | ||
| for i in n_list: | ||
| i=(i).capitalize() | ||
| cname.append(i) | ||
| return (" ".join(cname)) | ||
| print(name_check()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #Write a function that outputs the transcription of an input number with two digits. | ||
|
|
||
|
|
||
| def nr_reader(): | ||
| nr_list = list(input("Enter a double digits number : "))#we convert to list for indexing given number | ||
|
|
||
| x=int(nr_list[0]) #indexed units converted to integer,we will use as keys to call assigned values in dictionary | ||
| y=int(nr_list[1]) | ||
| #we need dictionaries to asiggn indexed keys to their string values | ||
| onluk={1:"on",2:"Yirmi",3:"Otuz",4:"Kirk",5:"Elli",6:"Altmis",7:"Yetmis",8:"Seksen",9:"Doksan"} | ||
| birlik={0:"",1:"bir",2:"iki",3:"uc",4:"dort",5:"bes",6:"alti",7:"yedi",8:"sekiz",9:"dokuz"} | ||
| #We use Turkish to make it clear :D | ||
| sayiyazi=onluk[x]+birlik[y] #calling values for each digit to print,adjacent values due to sum str | ||
| return(sayiyazi) | ||
| print(nr_reader()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clear |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
|
|
||
| """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). | ||
| This function finds perfect numbers between 1 and 1000. | ||
| And find the sum of these perfect numbers using reduce and filter functions additionally.""" | ||
|
|
||
|
|
||
| from functools import reduce | ||
| def perfect_nr(n): | ||
| sum = 0 | ||
| for x in range(1, n): | ||
| if n % x == 0: #checking positive divisors whether remainder is zero | ||
| sum += x #adding every each divisors | ||
| return sum == n #If given number is a perfect ,then this will be True | ||
| #filter function gathers perfect numbers(which return True) | ||
| print("Perfect numbers lower than 1000 :",list(filter(perfect_nr,range(2,1001)))) | ||
|
|
||
| print("Sum of perfect numbers lower than 1000 :",reduce(lambda x,y: x+y,list(filter(perfect_nr,range(2,1001))))) | ||
| # use reduce function to adding all perfect numbers lower than 1000. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perfect |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #We prevent user from repeating.Repeated elements in given list will be expelled. | ||
|
|
||
|
|
||
| def unique_list(): | ||
| elements=input("Avoiding repeating(split with comma ',') :").split(',') | ||
| # Initially we ask for enter list where elements splitted by coma | ||
| elements=set(elements) #Due to set property ,repeated element will be expelled. | ||
| elements=list(elements) #We revert set to list for print as a list. | ||
| return(elements) | ||
| print(unique_list()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clear