473,804 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help me to solve "Exchange Rates Problem"

4 New Member
Exchange Rates

Source file: Exchange.cs
Input file: Exchange.in
Output file: Exchange.out

Using money to pay for goods and services usually makes life easier, but sometimes people prefer to trade items directly without any money changing hands. In order to ensure a consistent "price", traders set an exchange rate between items. The exchange rate between two items A and B is expressed as two positive integers m and n, and says that m of item A is worth n of item B. For example, 2 stoves might be worth 3 refrigerators. (Mathematically , 1 stove is worth 1.5 refrigerators, but since it's hard to find half a refrigerator, exchange rates are always expressed using integers.)

Your job is to write a program that, given a list of exchange rates, calculates the exchange rate between any two items.

The input file contains one or more commands, followed by a line beginning with a period that signals the end of the file. Each command is on a line by itself and is either an assertion or a query. An assertion begins with an exclamation point and has the format
! m itema = n itemb
where itema and itemb are distinct item names and m and n are both positive integers less than 100. This command says that m of itema are worth n of itemb. A query begins with a question mark, is of the form
? itema = itemb
and asks for the exchange rate between itema and itemb, where itema and itemb are distinct items that have both appeared in previous assertions (although not necessarily the same assertion). For each query, output the exchange rate between itema and itemb based on all the assertions made up to that point. Exchange rates must be in integers and must be reduced to lowest terms. If no exchange rate can be determined at that point, use question marks instead of integers.
Format all output exactly as shown in the example.

Note:

Item names will have length at most 20 and will contain only lowercase letters. l
Only the singular form of an item name will be used (no plurals). l
There will be at most 60 distinct items. l
There will be at most one assertion for any pair of distinct items. l
There will be no contradictory assertions. For example, "2 pig = 1 cow", "2 cow = 1 horse", and "2 horse = 3 pig" are contradictory.
Assertions are not necessarily in lowest terms, but output must be.
Although assertions use numbers less than 100, queries may result in larger numbers that will
not exceed 10000 when reduced to lowest terms.


Example input

! 6 shirt = 15 sock
! 47 underwear = 9 pant
? sock = shirt
? shirt = pant
! 2 sock = 1 underwear
? pant = shirt
.
Example output:
5 sock = 2 shirt
? shirt = ? pant
45 pant = 188 shirt
Aug 13 '07 #1
5 3026
sicarie
4,677 Recognized Expert Moderator Specialist
Sure, what do you have so far?
Aug 13 '07 #2
annCooper
4 New Member
Sure, what do you have so far?
thanks for reply.....
I'm not able to understand how should i calculate values if they are present in 3 or 4 statements.
Aug 13 '07 #3
sicarie
4,677 Recognized Expert Moderator Specialist
thanks for reply.....
I'm not able to understand how should i calculate values if they are present in 3 or 4 statements.
The best way to figure something like that out, is to ignore the programming for a bit, and sit down and figure out how you would write instructions for a child to do it.

I would imagine you could go about this several ways. You could create a relation for each of the 60 items, or you could go through and build the relation for the current item (if you know which one you are searching for, you can search each line for that one. Then see if there is a direct comparison between the two. If there's not, go to the next on one, see if there is a valid comparison (if if they both compare to the same item), etc... until you have a match.

Are you having trouble with the logic (like above), or the implementation?
Aug 13 '07 #4
annCooper
4 New Member
The best way to figure something like that out, is to ignore the programming for a bit, and sit down and figure out how you would write instructions for a child to do it.

I would imagine you could go about this several ways. You could create a relation for each of the 60 items, or you could go through and build the relation for the current item (if you know which one you are searching for, you can search each line for that one. Then see if there is a direct comparison between the two. If there's not, go to the next on one, see if there is a valid comparison (if if they both compare to the same item), etc... until you have a match.

Are you having trouble with the logic (like above), or the implementation?
I'm clear with the logic, but I'm feeling difficulty in implementation.
Aug 13 '07 #5
sicarie
4,677 Recognized Expert Moderator Specialist
I'm clear with the logic, but I'm feeling difficulty in implementation.
Oh, ok, what is your algorithm?
Aug 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1281
by: anton muhin | last post by:
Hello, everybody! Trying to solve the problem in the subj, I found that I miss some iterator-related tools. Mostly consequental application of the same function to some argument (if I'm not missing something it has a name y-combinator). If we had one, generating the sequence of digits is easy: iter(y(lambda (q, _): divmod(q, n), (x, 0)).next, (0, 0))
5
9551
by: George Copeland | last post by:
This is a request for help fixing a SQL Server 2000/ADO problem on Windows XP. I would appreciate any useful assistance. PROBLEM: SQL Server access on my machine fails as follows: 1. All of my VB6 apps reference the following ADO typelib: Microsoft ActiveX Data Objects 2.7 Library Located at: c:\Program Files\Common Files\System\ADO\msado27.tlb
1
1301
by: Shapper | last post by:
Hello, I am accessing a value in a XML value: news.Load(Server.MapPath("xml/ news.rss")) newslabel.Text = CType(news.SelectSingleNode("rss version=&quot;2.0 &quot;/channel/title").InnerText, String) The XML file: <?xml version="1.0"?>
7
2007
by: Shapper | last post by:
Hello, I have an ASP:ImageButton where I want to call a function and pass a string: OnClick="Change_Photo("John")" I am having problems with "". I tried
9
9717
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a master-child link to the first subform. subform2 - Master Field: subTrainingModule.Form!TrainingModuleTopicSK Child Field: TrainingModuleTopicSK
7
38028
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement using innerHTML. Now, I've researched this problem on the web, and found many references to it, but none of them quite addressed my specific situation, and since my experience with JavaScript is limited, I was not able to adapt the solutions I...
17
12069
by: khajeddin | last post by:
the problem is: Create a class HugeInteger which use 40-element array of digits to store integers as larg as 40 digits each.Provide methods input, output, add, subtract how to add to arrays ?
8
21614
by: Steve Kershaw | last post by:
I have a debugging/breakpoint problem that I must fix before I move on!!! When I set a breakpoint in my ASP.NET (C#) page I get the red circle with the 'A'. When I mouse over this breakpoint I get one of two errors: First error "The breakpoint will not currently be hit. No symbols have been loaded for this document"
4
1849
Stang02GT
by: Stang02GT | last post by:
Hey guys, I was asked to help solve an issue another developer was having and I personally don't know much about it so I am turning to you guys for a little help/guidance. My co-worker has created an ASP page that is trying to pull the latest report from the network. And the ASP is failing when it tries to create the FSO to scan the files. A virtual directory has not been created yet so he is pointing directly to the network path. The...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.