
Soojung K.
asked 10/06/19How to write a and b as inclusive integers? (python3)
Write a program that takes three integers a, b, and k as command-line arguments writes the integers between a and b (both inclusive), but only k integers per row.
I honestly have no idea where to start, any help would be appreciated!
1 Expert Answer
Ihsan T. answered 10/08/19
Software Engineer & Master of Business Administration
Frankly speaking, it's a complex problem. But I think we manage it.
In my experience, it's always easier to divide and conquer the program. Let's digest this question a little: First, we need to write a program that can take 3 command line arguments. Only then we can proceed to solve the second part of the question. We need to find a way to write integers starting from the first number, up until the second number (inclusive) and do this by certain steps.
$ python test.py arg1 arg2 arg3
So, we need a way to take these arguments and convert them into variables for ourselves. Python has a great module to help us here. It's called "sys" module. Once we import the sys module, we can use its "argv" list variable to read into our program's arguments. Of course we should not forget that we can access that variable as "sys.argv".
Once we transfer our program's arguments into internal variables, we can start tackling the second portion of the question. How do you divite it into row. Well, putting aside the obvious checks and such, there's an interesting way to solve it.
We can take our first number and assign it to a variable, let's call it MyAwesomeNumber. We need to print every number starting from the first number until the second argument we have received. Let's assume that we printed all the numbers on the same row. Once we accomplish that, then we can do something really sneaky. We can keep track of how many numbers we have printed so far and after reaching our third argument we can print a line break.
I hope this gives you an idea about how to proceed. Have a great day programming!
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
David F.
What language is needed? C? Java? C++?10/06/19