This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

How would you go around this problem?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has already been coded. But I want to ask what would you guys do different? As in what methods would you use to tackle this problem and if there's better solutions than this.

A bicycle combination lock has four rings with numbers 0 through 9.
Given the actual numbers and the combination to unlock, print instructions
to unlock the lock using the minimum number of twists. A "twist up" increases
the number value of a ring, and a "twist down" decreases it. For example, if
the actual number shown is 1729 and the desired combination is 5714, write your
instructions in java like this:

Ring 1: Twist up 4 times
Ring 1: Twist up once
Ring 3: Twist down once
Ring 3: Twist down twice
 
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what your code is doing nor how it's supposed to be the solution to the given problem. I think your interpretation of the problem way off.

As I interpret the given problem, the program should print out instructions on how to open a combination lock with the fewest twists, given the lock's current state (the numbers shown) and the unlock combination. So if the lock is currently showing 1729 and its unlock combination is 5714, then the output should be something like this (assuming the rings are numbered from 1 to 4 starting from the left):

Ring 1: 4 twists up
Ring 2: 0 twists
Ring 3: 1 twist down
Ring 4: 5 twists up (or down)

Total twists needed to unlock: 10

Your program takes user input but it interprets the input as a ring number. Why? I would think that if there were to be any user input at all, it would be to ask the user to enter either the current setting (with a fixed unlock combo) or to enter both current setting and the unlock combo. There's no need for the user to enter ring numbers.
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the purpose of this method?

Do you realize this method will return true if the string you pass in was "brrrring" or "suffering" or "Pringles" or "Ringwald" or "The bell is Ringing"?
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

write your
instructions in java like this:

Ring 1: Twist up 4 times
Ring 1: Twist up once
Ring 3: Twist down once
Ring 3: Twist down twice



Are you sure that's what the instructions actually say? That doesn't make sense. Why would there be multiple instructions for the same ring?
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on my interpretation of the problem, I'd say the solution would be less than 80 lines of code. Here's an outline:
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the program must display "once" instead of "1 time" and "twice" instead of "2 times" then you'll have a few more lines of logic to account for those two special cases.
 
Richard Gonzales
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I don't understand what your code is doing nor how it's supposed to be the solution to the given problem. I think your interpretation of the problem way off.

As I interpret the given problem, the program should print out instructions on how to open a combination lock with the fewest twists, given the lock's current state (the numbers shown) and the unlock combination. So if the lock is currently showing 1729 and its unlock combination is 5714, then the output should be something like this (assuming the rings are numbered from 1 to 4 starting from the left):

Ring 1: 4 twists up
Ring 2: 0 twists
Ring 3: 1 twist down
Ring 4: 5 twists up (or down)

Total twists needed to unlock: 10

Your program takes user input but it interprets the input as a ring number. Why? I would think that if there were to be any user input at all, it would be to ask the user to enter either the current setting (with a fixed unlock combo) or to enter both current setting and the unlock combo. There's no need for the user to enter ring numbers.



This was a students work that I helped. I worked on it based on the students directions. I agree with you, it would have been less tedious to enter the current setting and unlock combo.
 
Richard Gonzales
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If the program must display "once" instead of "1 time" and "twice" instead of "2 times" then you'll have a few more lines of logic to account for those two special cases.



Yes,
 
Richard Gonzales
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I don't understand what your code is doing nor how it's supposed to be the solution to the given problem. I think your interpretation of the problem way off.

As I interpret the given problem, the program should print out instructions on how to open a combination lock with the fewest twists, given the lock's current state (the numbers shown) and the unlock combination. So if the lock is currently showing 1729 and its unlock combination is 5714, then the output should be something like this (assuming the rings are numbered from 1 to 4 starting from the left):

Ring 1: 4 twists up
Ring 2: 0 twists
Ring 3: 1 twist down
Ring 4: 5 twists up (or down)

Total twists needed to unlock: 10

Your program takes user input but it interprets the input as a ring number. Why? I would think that if there were to be any user input at all, it would be to ask the user to enter either the current setting (with a fixed unlock combo) or to enter both current setting and the unlock combo. There's no need for the user to enter ring numbers.



Also, I understand now. Based off of the current setting we can just print instructions on the screen indicating how many twists it takes for each ring to get to the unlock combination.
 
Richard Gonzales
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:What's the purpose of this method?

Do you realize this method will return true if the string you pass in was "brrrring" or "suffering" or "Pringles" or "Ringwald" or "The bell is Ringing"?



You're right. I should consider the length also.
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code seems to assume that the user enters a string that is "once" or "twice" -- that's totally the opposite of what the program is supposed to do.

The correct logic is (in pseudocode):

if (twists == 1) then display "once"
else if (twists == 2) then display "twice"
else display twists + " times"
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard Gonzales wrote:You're right. I should consider the length also.


I actually don't see the point of having that method at all. Again, the only input(s) to the program should be either the locked and unlocked combo or just the locked combo. That's the only user input that would make sense to me. You seem to think that the user is supposed to enter the ring number and the number of twists to turn each ring. Again, that's the opposite of what the problem is asking. The ring # and number of twists should be the program's output, not its input.
 
Richard Gonzales
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:That code seems to assume that the user enters a string that is "once" or "twice" -- that's totally the opposite of what the program is supposed to do.

The correct logic is (in pseudocode):

if (twists == 1) then display "once"
else if (twists == 2) then display "twice"
else display twists + " times"



Yeah, what I figured was that if the string contains "once" then int twist = 1;
if the string contains "twice" then int twist = 2;

twist being the number of times we twist each ring.
So if the current setting is 1000 and unlock is 3000
then an instruction would be Ring 1: twist up twice.
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard Gonzales wrote:
Yeah, what I figured was that if the string contains "once" then int twist = 1;
if the string contains "twice" then int twist = 2;


I'm still not following your reasoning.

Input comes before the output. "once" and "twice" are the outputs needed. Your logic implies a backwards line of reasoning, where "once" and "twice" are inputs and 1 and 2 are the outputs. That's wrong.

Also, what exactly is this "string" you're referring to that you are checking if it contains "once" or "twice"? There should be nothing of that sort. Again, the reasoning doesn't make sense to me.
 
Marshal
Posts: 24961
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand the instructions. Is the program supposed to take the lock settings and the unlock combination and output the instructions for unlocking the lock? Or is the user supposed to input the instructions and the program will tell them whether they unlocked the lock or not?

Your original post said

Given the actual numbers and the combination to unlock, print instructions to unlock the lock using the minimum number of twists.



That sounds like the first option, which is not all that difficult. But it seems to me you've programmed some version of the second option... I think... although I didn't go through all of that code to try and understand it. That's a lot more difficult.
 
Richard Gonzales
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I still don't understand the instructions. Is the program supposed to take the lock settings and the unlock combination and output the instructions for unlocking the lock? Or is the user supposed to input the instructions and the program will tell them whether they unlocked the lock or not?

Your original post said

Given the actual numbers and the combination to unlock, print instructions to unlock the lock using the minimum number of twists.



That sounds like the first option, which is not all that difficult. But it seems to me you've programmed some version of the second option... I think... although I didn't go through all of that code to try and understand it. That's a lot more difficult.



I didn't understand it either. The student just asked me to help them code the instructions and to capture users input so that it can change each ring individually. Initially, I was just going to have the user enter a combo to open the lock combo. It was my first time working with a student together as a non student myself lol.
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Given the actual numbers and the combination to unlock, print instructions
to unlock the lock using the minimum number of twists.


This seems pretty straightforward. The program is given two combinations: the current one (locked) and the combo to unlock it. Then print the instructions for each ring.
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a solution on repl.it -- you can run it here: https://quicklocksolver.jlacar.repl.run/

The unlock code has been hardcoded to 5714 for this solution but you can try different starting combinations to see the different instruction sets. I tested it a little bit and it seemed to be working correctly. To exit the loop, enter -1 when prompted for a combination.

The solution I wrote was 77 lines (including whitespace)
 
Richard Gonzales
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I wrote a solution on repl.it -- you can run it here: https://quicklocksolver.jlacar.repl.run/

The unlock code has been hardcoded to 5714 for this solution but you can try different starting combinations to see the different instruction sets. I tested it a little bit and it seemed to be working correctly. To exit the loop, enter -1 when prompted for a combination.

The solution I wrote was 77 lines (including whitespace)



That was very kind of you to do. Yep! this is also exactly what I was thinking as well. I'll try and redo it just for fun
 
Junilu Lacar
Sheriff
Posts: 14764
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I currently have. Made a few changes from the last time you tried it so the unlocked combination is also entered.

EDIT: and of course I've since refactored to make the names better. See the most recent edits here: https://repl.it/@jlacar/QuickLockSolver
 
Richard Gonzales
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Here's what I currently have. Made a few changes from the last time you tried it so the unlocked combination is also entered.



Sweet! I like it. Thanks for that! I'll try it again when I have some time and I'll compare with yours
I'm always looking to learn from others.

[Moderator edit: please don't quote entire posts, especially if they're long. Quote only the parts that are relevant to your reply]
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!