quantity conversion in python [on hold]
up vote
-1
down vote
favorite
I was given a question in an interview which is as follows.
Two input files are given
The first contains |N| ratios in the form of two labels and a ratio:
-------------------- ratios.csv --------------------
USD, GBP, 0.69
Meter, Yard, 1.09
YEN, EUR, 0.0077
GBP, YEN, 167.75
Horsepower, Watt, 745.7
Each line A, B, C means that C is a conversion factor from A to B. In other words, multiply by C to go from A to B, and 1 A is worth C B's.
Example: USD, GBP, 0.69 means that 1 USD = 0.69 GBP, so multiplying by 0.69 converts an amount from USD to GBP.
The second file contains |M| queries in the form of two labels
-------------------- queries.csv --------------------
USD,EUR
Yard,Meter
....
-----------------------------------------------------
And the expected output is a file with the query and the ratio value filled in.
-------------------- output.csv --------------------
USD, EUR, 0.89
Yard, Meter, 0.91
....
-----------------------------------------------------
Challenge: Write a program that reads both input files and produces the expected output file
and the answer I provided within (30 mns)
class Solution:
def __init__(self):
self.util_dict={}
self.util_dict.update({‘USD’:[((0.69), GBP)]})
self.util_dict.update({‘GBP’:[((1/0.69),USD),(167, YEN)]})
self.util_dict.update({‘EUR’:[(1/0.0077,YEN)]})
self.util_dict.update({‘YEN’:[(1/167,GBP),(0.0077,EUR)]})
self.util_dict.update({‘METER’:[(1.09,Yard)]})
self.util_dict.update({‘YARD’:[((1/1.09), Meter)]})
# USD,EUR
def calculateConv(self,qt1,qt2):
# input format [qt1,0]
visited=set()
q.append(qt1)
ans=1
count=0
while q:
k=q.pop() # USD
if k[0] in visited:
continue
if k==qt2:
ans*=k[1]
break
if count>1:
ans*=k[1] #0.69*167
if k in self.util_dict:
count+=1
keys=self.util_dict[qt1]
visited.add(k)
for c in range(keys):
q.append(c)
return ans
The idea is to save all the conversion factors in a dictionary. i.e if USD to GBP is 0.69 then GBP to USD is (1/0.69).
Loop through the values of key (USD) and multiply the value till we find the second quantity while keeping a track of visited keys.
is this solution not optimized or is there a better way to code this problem ? I understand there are bugs which I pointed out but what I want to know is is this approach wrong ?
python algorithm interview-questions
put on hold as off-topic by l0b0, Sᴀᴍ Onᴇᴌᴀ, 200_success, vnp, Jamal♦ yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – l0b0, Sᴀᴍ Onᴇᴌᴀ, 200_success, vnp, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-1
down vote
favorite
I was given a question in an interview which is as follows.
Two input files are given
The first contains |N| ratios in the form of two labels and a ratio:
-------------------- ratios.csv --------------------
USD, GBP, 0.69
Meter, Yard, 1.09
YEN, EUR, 0.0077
GBP, YEN, 167.75
Horsepower, Watt, 745.7
Each line A, B, C means that C is a conversion factor from A to B. In other words, multiply by C to go from A to B, and 1 A is worth C B's.
Example: USD, GBP, 0.69 means that 1 USD = 0.69 GBP, so multiplying by 0.69 converts an amount from USD to GBP.
The second file contains |M| queries in the form of two labels
-------------------- queries.csv --------------------
USD,EUR
Yard,Meter
....
-----------------------------------------------------
And the expected output is a file with the query and the ratio value filled in.
-------------------- output.csv --------------------
USD, EUR, 0.89
Yard, Meter, 0.91
....
-----------------------------------------------------
Challenge: Write a program that reads both input files and produces the expected output file
and the answer I provided within (30 mns)
class Solution:
def __init__(self):
self.util_dict={}
self.util_dict.update({‘USD’:[((0.69), GBP)]})
self.util_dict.update({‘GBP’:[((1/0.69),USD),(167, YEN)]})
self.util_dict.update({‘EUR’:[(1/0.0077,YEN)]})
self.util_dict.update({‘YEN’:[(1/167,GBP),(0.0077,EUR)]})
self.util_dict.update({‘METER’:[(1.09,Yard)]})
self.util_dict.update({‘YARD’:[((1/1.09), Meter)]})
# USD,EUR
def calculateConv(self,qt1,qt2):
# input format [qt1,0]
visited=set()
q.append(qt1)
ans=1
count=0
while q:
k=q.pop() # USD
if k[0] in visited:
continue
if k==qt2:
ans*=k[1]
break
if count>1:
ans*=k[1] #0.69*167
if k in self.util_dict:
count+=1
keys=self.util_dict[qt1]
visited.add(k)
for c in range(keys):
q.append(c)
return ans
The idea is to save all the conversion factors in a dictionary. i.e if USD to GBP is 0.69 then GBP to USD is (1/0.69).
Loop through the values of key (USD) and multiply the value till we find the second quantity while keeping a track of visited keys.
is this solution not optimized or is there a better way to code this problem ? I understand there are bugs which I pointed out but what I want to know is is this approach wrong ?
python algorithm interview-questions
put on hold as off-topic by l0b0, Sᴀᴍ Onᴇᴌᴀ, 200_success, vnp, Jamal♦ yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – l0b0, Sᴀᴍ Onᴇᴌᴀ, 200_success, vnp, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
1
I don't understand how your sample output file got generated. How are you supposed to come up with USD, EUR, 0.89 from the query and ratio.csv?
– Ywapom
yesterday
@Ywapom Convert USD → GBP → YEN → EUR.
– l0b0
yesterday
3
You have at least one syntax error in your code (typographic quotes). Please fix that and update your question.
– l0b0
yesterday
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I was given a question in an interview which is as follows.
Two input files are given
The first contains |N| ratios in the form of two labels and a ratio:
-------------------- ratios.csv --------------------
USD, GBP, 0.69
Meter, Yard, 1.09
YEN, EUR, 0.0077
GBP, YEN, 167.75
Horsepower, Watt, 745.7
Each line A, B, C means that C is a conversion factor from A to B. In other words, multiply by C to go from A to B, and 1 A is worth C B's.
Example: USD, GBP, 0.69 means that 1 USD = 0.69 GBP, so multiplying by 0.69 converts an amount from USD to GBP.
The second file contains |M| queries in the form of two labels
-------------------- queries.csv --------------------
USD,EUR
Yard,Meter
....
-----------------------------------------------------
And the expected output is a file with the query and the ratio value filled in.
-------------------- output.csv --------------------
USD, EUR, 0.89
Yard, Meter, 0.91
....
-----------------------------------------------------
Challenge: Write a program that reads both input files and produces the expected output file
and the answer I provided within (30 mns)
class Solution:
def __init__(self):
self.util_dict={}
self.util_dict.update({‘USD’:[((0.69), GBP)]})
self.util_dict.update({‘GBP’:[((1/0.69),USD),(167, YEN)]})
self.util_dict.update({‘EUR’:[(1/0.0077,YEN)]})
self.util_dict.update({‘YEN’:[(1/167,GBP),(0.0077,EUR)]})
self.util_dict.update({‘METER’:[(1.09,Yard)]})
self.util_dict.update({‘YARD’:[((1/1.09), Meter)]})
# USD,EUR
def calculateConv(self,qt1,qt2):
# input format [qt1,0]
visited=set()
q.append(qt1)
ans=1
count=0
while q:
k=q.pop() # USD
if k[0] in visited:
continue
if k==qt2:
ans*=k[1]
break
if count>1:
ans*=k[1] #0.69*167
if k in self.util_dict:
count+=1
keys=self.util_dict[qt1]
visited.add(k)
for c in range(keys):
q.append(c)
return ans
The idea is to save all the conversion factors in a dictionary. i.e if USD to GBP is 0.69 then GBP to USD is (1/0.69).
Loop through the values of key (USD) and multiply the value till we find the second quantity while keeping a track of visited keys.
is this solution not optimized or is there a better way to code this problem ? I understand there are bugs which I pointed out but what I want to know is is this approach wrong ?
python algorithm interview-questions
I was given a question in an interview which is as follows.
Two input files are given
The first contains |N| ratios in the form of two labels and a ratio:
-------------------- ratios.csv --------------------
USD, GBP, 0.69
Meter, Yard, 1.09
YEN, EUR, 0.0077
GBP, YEN, 167.75
Horsepower, Watt, 745.7
Each line A, B, C means that C is a conversion factor from A to B. In other words, multiply by C to go from A to B, and 1 A is worth C B's.
Example: USD, GBP, 0.69 means that 1 USD = 0.69 GBP, so multiplying by 0.69 converts an amount from USD to GBP.
The second file contains |M| queries in the form of two labels
-------------------- queries.csv --------------------
USD,EUR
Yard,Meter
....
-----------------------------------------------------
And the expected output is a file with the query and the ratio value filled in.
-------------------- output.csv --------------------
USD, EUR, 0.89
Yard, Meter, 0.91
....
-----------------------------------------------------
Challenge: Write a program that reads both input files and produces the expected output file
and the answer I provided within (30 mns)
class Solution:
def __init__(self):
self.util_dict={}
self.util_dict.update({‘USD’:[((0.69), GBP)]})
self.util_dict.update({‘GBP’:[((1/0.69),USD),(167, YEN)]})
self.util_dict.update({‘EUR’:[(1/0.0077,YEN)]})
self.util_dict.update({‘YEN’:[(1/167,GBP),(0.0077,EUR)]})
self.util_dict.update({‘METER’:[(1.09,Yard)]})
self.util_dict.update({‘YARD’:[((1/1.09), Meter)]})
# USD,EUR
def calculateConv(self,qt1,qt2):
# input format [qt1,0]
visited=set()
q.append(qt1)
ans=1
count=0
while q:
k=q.pop() # USD
if k[0] in visited:
continue
if k==qt2:
ans*=k[1]
break
if count>1:
ans*=k[1] #0.69*167
if k in self.util_dict:
count+=1
keys=self.util_dict[qt1]
visited.add(k)
for c in range(keys):
q.append(c)
return ans
The idea is to save all the conversion factors in a dictionary. i.e if USD to GBP is 0.69 then GBP to USD is (1/0.69).
Loop through the values of key (USD) and multiply the value till we find the second quantity while keeping a track of visited keys.
is this solution not optimized or is there a better way to code this problem ? I understand there are bugs which I pointed out but what I want to know is is this approach wrong ?
python algorithm interview-questions
python algorithm interview-questions
edited yesterday
l0b0
3,847923
3,847923
asked yesterday
Rohit
5073819
5073819
put on hold as off-topic by l0b0, Sᴀᴍ Onᴇᴌᴀ, 200_success, vnp, Jamal♦ yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – l0b0, Sᴀᴍ Onᴇᴌᴀ, 200_success, vnp, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by l0b0, Sᴀᴍ Onᴇᴌᴀ, 200_success, vnp, Jamal♦ yesterday
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – l0b0, Sᴀᴍ Onᴇᴌᴀ, 200_success, vnp, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
1
I don't understand how your sample output file got generated. How are you supposed to come up with USD, EUR, 0.89 from the query and ratio.csv?
– Ywapom
yesterday
@Ywapom Convert USD → GBP → YEN → EUR.
– l0b0
yesterday
3
You have at least one syntax error in your code (typographic quotes). Please fix that and update your question.
– l0b0
yesterday
add a comment |
1
I don't understand how your sample output file got generated. How are you supposed to come up with USD, EUR, 0.89 from the query and ratio.csv?
– Ywapom
yesterday
@Ywapom Convert USD → GBP → YEN → EUR.
– l0b0
yesterday
3
You have at least one syntax error in your code (typographic quotes). Please fix that and update your question.
– l0b0
yesterday
1
1
I don't understand how your sample output file got generated. How are you supposed to come up with USD, EUR, 0.89 from the query and ratio.csv?
– Ywapom
yesterday
I don't understand how your sample output file got generated. How are you supposed to come up with USD, EUR, 0.89 from the query and ratio.csv?
– Ywapom
yesterday
@Ywapom Convert USD → GBP → YEN → EUR.
– l0b0
yesterday
@Ywapom Convert USD → GBP → YEN → EUR.
– l0b0
yesterday
3
3
You have at least one syntax error in your code (typographic quotes). Please fix that and update your question.
– l0b0
yesterday
You have at least one syntax error in your code (typographic quotes). Please fix that and update your question.
– l0b0
yesterday
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
1
I don't understand how your sample output file got generated. How are you supposed to come up with USD, EUR, 0.89 from the query and ratio.csv?
– Ywapom
yesterday
@Ywapom Convert USD → GBP → YEN → EUR.
– l0b0
yesterday
3
You have at least one syntax error in your code (typographic quotes). Please fix that and update your question.
– l0b0
yesterday