6) Design a control circuit that will allow you to appropriately display the sum
ID: 2249259 • Letter: 6
Question
6) Design a control circuit that will allow you to appropriately display the sum on the seven segment displays. For example adding 1 + 8 gives 9 and you will display 09, adding 1 9 gives 10 and you must display 10, adding 9+ 12 gives 21 and you must display 21 while adding 15 + 15 gives 30 and you must display 30. Hint create a truth table to get a sense of the bits that change for sum going from a range of 00 to 09 to the range 10 to 19 and from the range 10 to 19 to the range 20 to 29 and final 30. Use these bits that change for your control circuitExplanation / Answer
Max starts to type all the values from a to b. After typing each number Max resets the calculator. Find the total number of segments printed on the calculator.
For example if a=1 and b=3 then at first the calculator will print 2 segments, then — 5 segments and at last it will print 5 segments. So the total number of printed segments is 12.
Input
The only line contains two integers a,b (1ab106) — the first and the last number typed by Max.
Output
Print the only integer a — the total number of printed segments.
Examples
input
1 3
output
12
input
10 15
output
39
Here's my attempt:
When I submit the code, it fails on a timeout for a test case (unknown test case). How can I make it more efficient? All tests should run in under 1 second.
One idea I've had is to store the numbers not in the "one's place" - these won't change.
For instance: in the sequence 10, 11, 12, 13, 14, ..., I don't need to lookup 1 each time. I only need to look at the last digit until I see a 9.
Isolate calculation from I/O
The main routine does both the input and output and also is materially involved in the main calculation. I'd advocate that that function should be isolated like this:
Use const where practical
The segment_count map shouldn't be changed once constructed, and so it should be declared const.
Avoid global variables
The segment_count variable is only used within get_number_of_segments(), so it should go there instead of being a global. I would also avoid the overhead of the std::unordered_map by using a simple array:
A better algorithm
Your existing code, while not the fastest possible, does have a significant advantage in that it is obviously correct. We can use that to verify any alternative approach as well as using it for timing comparisons. From here to the end of this review, I'll be showing stepwise improvements in the code.
Write a test harness
We might write several versions of the code and want to compare them. One nice way to do that is using a templated structure and a macro like this:
Now we can easily make an array of tests and run through all alternative algorithms:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.