辅导data编程、讲解Python,Java程序、c/c++程序讲解
- 首页 >> Matlab编程 Question #2: (10 marks)
Write a function called H_diamond that takes an integer size as a parameter and prints a size * 2 – 1 wide by size*2 tall diamond of the letter H (as in Hamzah).
For example, if you enter integer: 6, you should get:
Question #3: (10 marks)
Hamzah wants to stop water wastage by creating a new billing system that will make people pay more the more water they use.
Write a function called WaterBill2021() that takes as input the daily usage in liters of water and number of people living in the house then approximates the amount payable following those rules.
Depending on the number of the household members the first 3 litters for each member is free (3 * number of members). Since the daily water intake for humans is ~3L.
For the rest each extra 10 litters will cost: 12$ + 3% of the total cost of all the litters which had been used before. (The first 10 litters used after the free ones will cost only 12$, because the total cost before was 0).
The last few litters (less than 10) each will cost 2$
For example if you entre 100 litters and 5 members, you should get:
Enter litters used daily: 100
Enter number of household members: 5
amount of free water = 15
116.70803255504644
Question #4: (20 marks)
Write a function named shortest_name that reads a number of names (as an input), typed by the user and prints the shortest name (the name that contains the least characters):
Your method should accept an integer n as a parameter and should then prompt for n names.
The shortest name should be printed with its first letter in lowercase and all subsequent letters capitalized, regardless of the capitalization the user used when typing in the name.
If there is a tie for shortest between two or more names, use the tied name that was typed earliest.
Also print a message saying that there was a tie, as shown below.
It is possible that some longer names will tie in length, such as Wassim and hamzah in the example below; but don't print a message unless the tie is between the shortest names.
You may assume that n is at least 1, that each name is at least 1 character long and at most 30 characters long, and that the user will type single-word names consisting of only letters.
Example #1:
Enter number of names :5
Enter Name #1: Leah
Enter Name #2: Liv
Enter Name #3: Lexi
Enter Name #4: Lex
Enter Name #5: Lily
lIV's Name is the shortest
(There was a Tie!)
Example #2:
Enter number of names :3
Enter Name #1: hamzah
Enter Name #2: wassim
Enter Name #3: sara
sARA's Name is the shortest
Question #5: (10 marks)
Write function vowel_bingo(S) that takes a string as input and counts the number of occurrences of vowels in each string.
If the string has more than 5 vowels, then the function should print “whammy you lost”. Then exit.
If all the 5 vowels appeared in the string, then instead of a whammy the function should print “bingo you have won”. Then exist.
If the number of vowels is less than 5 the function should print “playing it safe, eh?”. Then exit.
Examples :
Enter String: Le Tour de France
whammy you lost
Enter String: EDUCATION
bingo you have won
Enter String: hamzah
playing it safe, eh?
Question # 6: (20 marks)
Hamzah believes that a good password is a password that you can remember while it also meets all the requirements of a website. So, he created a way to generate a unique password for each website you visit. The way to generate a password is given below:
Write your mother’s year of birth while pressing shift to generate special characters. For example, 1956 would be typed as !(%^ when you hold shift.
Write your name: “Hamzah”.
Write the street number of your grandfather/grandmother house: “674”.
Write your favorite movie: “Star Wars”.
Write your bf/gf/bff name: “Sara”.
Write something related to the website: e.g., for google: “android”.
Remove all spaces from all the strings above. Uppercase your name while force the rest to be lowercase.
Now with the new Strings you can create your new password for google:
Start with the first 2 letters of your name “HA”. The last two letters from your favorite movie “rs”. The last two letters from your bf/gf/bff name “sa”. The last two letters from your related word “id”
“HA”+”rs”+”sa”+”id”
Between each of the 4 strings add one character from both the street number and birthday. You can assume that the birth year is always 4 digits and the street address is always 3 digits.
“HA”+”!6”+”rs”+”(7”+”sa”+”%4”+”id”+”^”
The new password should be a string of all of those parts : “HA!6rs(7sa%4id^”
Write a function called test_hamzah_password() that takes any user input password and verify if the password was created through hamzah’s logic or not. The function should ask the user to enter all the original strings and then verify that the entered password is a sum of all the parts of the entered strings.
your function should print statements tells if the password is done using hamzah’s way or not.
Example 1:
Enter your password: HA!6rs(7sa%4id^
Enter your mother’s year of birth while pressing shift: !(%^
Enter your name: Hamzah
Enter the street number of your grandfather/grandmother house: 674
Enter your favorite movie: Star Wars
Enter your bf/gf/bff name: Sara
Enter something related to the website: android
Great, your password is done using hamzah’s way
Example 2:
Enter your password: WA!5ay&5Wd#
Enter your mother’s year of birth while pressing shift: !(^!
Enter your name: Wassim
Enter the street number of your grandfather/grandmother house: 753
Enter your favorite movie: the godfather
Enter your bf/gf/bff name: ayah
Enter something related to the website: Iphone
Try again, your password was not done using hamzah’s way
Question # 7: (10 marks)
Lossy compression/encoding/encryption is a class of methods where the loss of some of the data is desired (usually to reduce the size of the data). A good example of this is images/Videos as most of the videos you get from the internet have gone through this and lost a large amount of the original data ( though you will not be able to see the difference since those methods tries remove unnoticeable things. You can google this subject to learn more.
Write a simple function called lossy_encryption( ) that takes input string and it should returns new string (encrypted version of input string).
Your function should take a triplet and delete the middle character while reversing the order of the other two. In case your string length is not a multiple of three, add the last one/two characters to the end of the encrypted string.
To validate your solution, if you use input " 123456 ", your function must return "3164"
Question # 8: (10 marks)
Write a simple function called lossy_decryption( ) that takes input string (the encrypted one in question #8 above) and it should return a copy similar to the original but with “*” in place of the lost characters .
To validate your solution, if you use input "3164", your function must return "1*34*6 ". notice that we can guess that the original string was “123456”, most of lossy decryption methods do some guess work.
Write a function called H_diamond that takes an integer size as a parameter and prints a size * 2 – 1 wide by size*2 tall diamond of the letter H (as in Hamzah).
For example, if you enter integer: 6, you should get:
Question #3: (10 marks)
Hamzah wants to stop water wastage by creating a new billing system that will make people pay more the more water they use.
Write a function called WaterBill2021() that takes as input the daily usage in liters of water and number of people living in the house then approximates the amount payable following those rules.
Depending on the number of the household members the first 3 litters for each member is free (3 * number of members). Since the daily water intake for humans is ~3L.
For the rest each extra 10 litters will cost: 12$ + 3% of the total cost of all the litters which had been used before. (The first 10 litters used after the free ones will cost only 12$, because the total cost before was 0).
The last few litters (less than 10) each will cost 2$
For example if you entre 100 litters and 5 members, you should get:
Enter litters used daily: 100
Enter number of household members: 5
amount of free water = 15
116.70803255504644
Question #4: (20 marks)
Write a function named shortest_name that reads a number of names (as an input), typed by the user and prints the shortest name (the name that contains the least characters):
Your method should accept an integer n as a parameter and should then prompt for n names.
The shortest name should be printed with its first letter in lowercase and all subsequent letters capitalized, regardless of the capitalization the user used when typing in the name.
If there is a tie for shortest between two or more names, use the tied name that was typed earliest.
Also print a message saying that there was a tie, as shown below.
It is possible that some longer names will tie in length, such as Wassim and hamzah in the example below; but don't print a message unless the tie is between the shortest names.
You may assume that n is at least 1, that each name is at least 1 character long and at most 30 characters long, and that the user will type single-word names consisting of only letters.
Example #1:
Enter number of names :5
Enter Name #1: Leah
Enter Name #2: Liv
Enter Name #3: Lexi
Enter Name #4: Lex
Enter Name #5: Lily
lIV's Name is the shortest
(There was a Tie!)
Example #2:
Enter number of names :3
Enter Name #1: hamzah
Enter Name #2: wassim
Enter Name #3: sara
sARA's Name is the shortest
Question #5: (10 marks)
Write function vowel_bingo(S) that takes a string as input and counts the number of occurrences of vowels in each string.
If the string has more than 5 vowels, then the function should print “whammy you lost”. Then exit.
If all the 5 vowels appeared in the string, then instead of a whammy the function should print “bingo you have won”. Then exist.
If the number of vowels is less than 5 the function should print “playing it safe, eh?”. Then exit.
Examples :
Enter String: Le Tour de France
whammy you lost
Enter String: EDUCATION
bingo you have won
Enter String: hamzah
playing it safe, eh?
Question # 6: (20 marks)
Hamzah believes that a good password is a password that you can remember while it also meets all the requirements of a website. So, he created a way to generate a unique password for each website you visit. The way to generate a password is given below:
Write your mother’s year of birth while pressing shift to generate special characters. For example, 1956 would be typed as !(%^ when you hold shift.
Write your name: “Hamzah”.
Write the street number of your grandfather/grandmother house: “674”.
Write your favorite movie: “Star Wars”.
Write your bf/gf/bff name: “Sara”.
Write something related to the website: e.g., for google: “android”.
Remove all spaces from all the strings above. Uppercase your name while force the rest to be lowercase.
Now with the new Strings you can create your new password for google:
Start with the first 2 letters of your name “HA”. The last two letters from your favorite movie “rs”. The last two letters from your bf/gf/bff name “sa”. The last two letters from your related word “id”
“HA”+”rs”+”sa”+”id”
Between each of the 4 strings add one character from both the street number and birthday. You can assume that the birth year is always 4 digits and the street address is always 3 digits.
“HA”+”!6”+”rs”+”(7”+”sa”+”%4”+”id”+”^”
The new password should be a string of all of those parts : “HA!6rs(7sa%4id^”
Write a function called test_hamzah_password() that takes any user input password and verify if the password was created through hamzah’s logic or not. The function should ask the user to enter all the original strings and then verify that the entered password is a sum of all the parts of the entered strings.
your function should print statements tells if the password is done using hamzah’s way or not.
Example 1:
Enter your password: HA!6rs(7sa%4id^
Enter your mother’s year of birth while pressing shift: !(%^
Enter your name: Hamzah
Enter the street number of your grandfather/grandmother house: 674
Enter your favorite movie: Star Wars
Enter your bf/gf/bff name: Sara
Enter something related to the website: android
Great, your password is done using hamzah’s way
Example 2:
Enter your password: WA!5ay&5Wd#
Enter your mother’s year of birth while pressing shift: !(^!
Enter your name: Wassim
Enter the street number of your grandfather/grandmother house: 753
Enter your favorite movie: the godfather
Enter your bf/gf/bff name: ayah
Enter something related to the website: Iphone
Try again, your password was not done using hamzah’s way
Question # 7: (10 marks)
Lossy compression/encoding/encryption is a class of methods where the loss of some of the data is desired (usually to reduce the size of the data). A good example of this is images/Videos as most of the videos you get from the internet have gone through this and lost a large amount of the original data ( though you will not be able to see the difference since those methods tries remove unnoticeable things. You can google this subject to learn more.
Write a simple function called lossy_encryption( ) that takes input string and it should returns new string (encrypted version of input string).
Your function should take a triplet and delete the middle character while reversing the order of the other two. In case your string length is not a multiple of three, add the last one/two characters to the end of the encrypted string.
To validate your solution, if you use input " 123456 ", your function must return "3164"
Question # 8: (10 marks)
Write a simple function called lossy_decryption( ) that takes input string (the encrypted one in question #8 above) and it should return a copy similar to the original but with “*” in place of the lost characters .
To validate your solution, if you use input "3164", your function must return "1*34*6 ". notice that we can guess that the original string was “123456”, most of lossy decryption methods do some guess work.