r/dailyprogrammer • u/nint22 1 2 • Jun 09 '13
[06/09/13] Challenge #127 [Intermediate] Call Forwarding
(Intermediate): Call Forwarding
A call forwarding service is a system that allows any incoming phone calls to a phone number be forwarded to a secondary phone number. This system is helpful in the case of a person taking a vacation (so that if Alice is out of the office, Bob receives all her customer's calls). It is possible, with such a system, that the secondary receiver (Bob in this case) also goes on vacation and also setups call forwarding to another person (Carol). Thus in such a situation, if someone calls Alice, it gets forwarded to Bob who in turn has the system re-forward to Carol.
Your job is to implement such a system, take in people's vacation times, and return how many call forwards are implemented at a given time and how "deep" the forwarding goes.
Special thanks to the ACM collegiate programming challenges group for giving me the initial idea here. Also, based on recent world news, please consider donating to the EFF and make sure to write good code that protects your users. This subreddit is not the right place for a political discussion; I leave it up to the reader to think about why/how this subject may be important to you. At least consider that software you write in your "real-world job" may be used by an international audience, and such an audience may be targeted by unscrupulous people/governments. Protect people's lives by protecting their digital data: we programmers are the few people who can actually protect our users. </preachy paragraph>
Formal Inputs & Outputs
Input Description
You will be given an integer N on its own line that represents the number of vacation schedule descriptions that follow (each on their separate line). For each vacation description, you will be given four integers: the first is the person's regular 4-digit phone number, then the 4-digit phone number they choose to forward to, then when the vacation starts (measured in days) and how long the vacation lasts (measured in days). On the final line of input, which is line N + 1, you will be given a day to test the properties of the call-forwarding system (as defined in the output description).
Note that the date/time system here is based on a day index system. 1 represents the first day, 2 represents the second day, etc. Days do not respect the concept of months or years, so expect to simulate any given schedule up to the day 4,294,967,295. (32-bit unsigned integer max value)
Note that the input's forwarding chain will be guaranteed to not have circular forwarding: you can expect that Carol, in the challenge description, will never re-forward back to Alice while she is on vacation. As a secondary challenge, if you can detect such a failure, in that case simply print the chain in question that fails the call forwarding.
Output Description
For the given day you want to test the system (the last integer from the input format), you must print both how many call forwarding are in place and the largest forwarding chain. A forwarding chain is the relationship as described in the challenge description where Alice forwards to Bob, who in turn forwards to Carol (this chain has a value of two, for the two call forwards).
Sample Inputs & Outputs
Sample Input
3
0000 0001 1 3
0001 4964 2 1
4964 0005 2 3
2Sample Output
3 call forwardings set up on day 2
3 call forwardings are the longest chain on day 2
4
u/zck Jun 09 '13 edited Jun 09 '13
Isn't the sample output wrong? On day 2, 0 is forwarded to 1. 1 is forwarded to 4964, and 4964 is forwarded to 5. That's three forwardings in the longest chain, not 2.Edit: nint22 fixed it in the post. This was wrong in the original posting, so if you're confused that your code doesn't match up to what it should be, make sure the sample you're looking at has 3 forwardings, not 2.
If that's correct, so is my code. I wrote in Arc. Try it here!
I'm thrilled to answer questions about what I think is the most beautiful programming language around.
Edit: let's not let things horizontally scroll, please.
Edit edit: why don't we test the code I'm suggesting you use.
read-stringwas broken. Now it's fixed.2
u/tchakkazulu 0 2 Jun 09 '13
EDIT: Ninja-fixed, as I was pondering what I could've missed. Cheers, nint22!
I don't understand why the longest chain on day 2 is only 2 long. 0000, 0001, and 4964 are all on vacation, so the longest chain is 0000->0001->4964->0005? What am I missing?4
u/ILiftOnTuesdays 1 0 Jun 10 '13
Python time! Includes circular forward detection. (I wasn't sure what to do with it, so I just threw a warning and considered that chain to be as long as the loop.
3
u/skeeto -9 8 Jun 09 '13 edited Jun 10 '13
JavaScript,
Usage:
3
u/jiri_jakes Jun 10 '13 edited Jun 10 '13
Scala, REPLable. Does not detect loops.
Output:
3
u/cooptex977 Jun 12 '13 edited Jun 12 '13
C#. I rather like this solution, but feedback is always welcome, and I would appreciate it if I got some. Thanks :) EDIT: Sorry I'm a bit late to the party. I hope someone sees this anyway :/
2
u/TweenageDream Jun 10 '13
Here is my solution in Ruby
Output:
2
u/[deleted] Jun 10 '13 edited Jun 10 '13
Python. I didn't include circular detection. It takes a text file as input. Experienced programmers, feel free to critique my code.
2
u/tchakkazulu 0 2 Jun 10 '13
My obligatory Haskell code, which does not check for loops. It has some trippy stuff going on for people who're not used to lazyness. Check out the definition of
graphinsidecollect.Note: the program itself does not explicitly check for loops, so there won't be any sensible output in that case apart from the amount of call forwardings. However, GHC's runtime system is able to detect running around in circles in this case, and will crash out of the program with error message "<<loop>>".
2
u/Amneamnius Jun 10 '13
C++ code on GitHub
Output:
2
u/Quasimoto3000 1 0 Oct 06 '13 edited Oct 09 '13
A Memoized clojure solution. Im just starting out with clojure so I would love some feedback!
Run with: (main "input.txt")
Can someone let me know if I used
memoizeright here?2
u/godzab Oct 26 '13
Tried this in Java, but not sure if I got it:
}
1
u/kalgynirae Jun 10 '13 edited Jun 10 '13
Python 3. It does detect circular forwarding (it just prints the chain and gives up).
EDIT: added some blank lines for readability.
1
u/5hassay Jun 10 '13
here is my Python33 solution that does not check for loops
1
u/oasisguy Jun 11 '13 edited Jun 11 '13
C++, using a map of maps. Detects and warns about loops, and comes with an ugly input interface.
1
u/altanic Jun 12 '13
c# w/ circular detection
sample input gives sample output & here's a sample of it detecting a circular loop:
Input:
Output:
1
u/asthasr Jun 13 '13 edited Jun 13 '13
A Scala solution, compilable and runnable from the command line.
I'm not particularly happy with the input handling, but this seems like an awkward part of Scala. I will likely think about it a bit more.
1
u/crawphish Jun 13 '13
Python solution using a weird linked-list/dictionary thing (Note: I didnt use input from .txt file, I just hard coded it in):
Output:
1
u/36912 Jun 17 '13
Here's my solution in Python I thought a little about how best I could store the schedule information and go about solving before settling for a dictionary. I'd love feedback!
1
u/Loomax Jun 18 '13
I'm really late to this, but there was no Java solution yet and I think this challenge is quite interesting. I'd appreciate any feedback especially on the chain finding. It does not discover circular chains yet. Whole project is hosted at github, but I'm trying to get the important parts here.
Searching the chain:
The PhoneForward Class which basically encapsulates a line of input
1
u/chaz2x4 Jun 30 '13 edited Jun 30 '13
Really late, but I thought I'd give it a try. First time I looked at it, I couldn't understand.
But now!
JAVA:
EDIT: This only works if the forwarding is in order. Example 0000 -> 0001, 0001 -> 4625, so on. If there is 0000 -> 0001, 0265 -> 5531, 0001 -> 4625, the forwarding chain will not work. I'll try to fix this in my code.