This is not Python ! It is OCAML FUNCTION PROGRAMMING LANGUAGE NEED CODE IN OCAM
ID: 3838134 • Letter: T
Question
This is not Python ! It is OCAML FUNCTION PROGRAMMING LANGUAGE NEED CODE IN OCAML. Please help me this thank you!
Q1. Write a function to build a dictionary from two lists of equal length. One of the lists should contain elements that will serves as keys and the other list should contain elements that will serve as values.
Run your code to demonstrate that it works. Include the output in your answer. The output should show the two starting lists and the resulting dictionary.
What happens if the lists should chance to be of unequal length?
Q2. Now write a function that will do the inverse of the operation above i.e. this function takes a dictionary and returns two lists, one list containing keys and the other list containing values.
Can you really return two independent lists (i.e. can a function return two values)? How will you resolve this problem?
Run your code to demonstrate that it works. Include the output in your answer. The output should show the starting dictionary and the two resulting lists.
Assignment Submission Instructions
Submit your work as two files:
A .ml or .caml code file (which must execute without warnings or errors)
A Microsoft Word document containing the contents of your code file (see further instructions below
Name your files as follows: YourFirstName_YourLastName_E4 (.ml or .caml, and .doc or .doctx)
In your code file, put your name, assignment name, and the date the assignment work must be submitted, at the top.
The questions and your explanations/reasoning must be included as comments in your code listing. The comments must:
Respond to any questions asked within the primary questions given above
Explain the functioning of the code segments you have written
Provide your reasoning for why you chose to write the code the way you did and if you considered any alternate ways of writing it
Explanation / Answer
Q1: A dictionary is an associative array (also known as hashes). Any key of the dictionary is associated (or mapped) to a value. The values of a dictionary can be any Python data type.. Dictionaries don't support the sequence operation of the sequence data types like strings, tuples and lists.
keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary)
{'a': 1, 'b': 2, 'c': 3}
if the output is of unequal length
a = [1,2,3]
b = [9,10]
for i in itertools.izip_longest(a,b): print i
...
(1, 9)
(2, 10)
(3, None)
If you really want to get rid of the Nones, then you could try:
for i in (filter(None, pair) for pair in itertools.izip_longest(a,b)): print i
(1, 9)
(2, 10)
(3,)
Q2:
d = ReversibleDict({'a': 3, 'c': 2, 'b': 2, 'e': 3, 'd': 1, 'f': 2})
d.reversed()
{1: ['d'], 2: ['c', 'b', 'f'], 3: ['a', 'e']}
returning two indepentent list
def select_choice():
loop = 1
row = 0
while loop == 1:
print('''Choose from the following options?:
1. Row 1
2. Row 2
3. Row 3''')
row = int(input("Which row would you like to move the card from?: "))
if row == 1:
i = 2
card = list_a[-1]
elif row == 2:
i = 1
card = list_b[-1]
elif row == 3:
i = 0
card = list_c[-1]
return i
return card
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.