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
• Liutauras Vilda
• Junilu Lacar
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Knute Snortum
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Frits Walraven
• Ganesh Patekar

# Unclear line in code

Ranch Foreman
Posts: 266
12
• 1
**edit:
I edited the title of this post. It seems that I have been unclear, and people started to fix everything.
**

Hello everyone!

The code below is in Python, but I hope you will forgive me, as the question is actually a programming-beginners question and not a syntax issue.

The problem is as follow:
https://open.kattis.com/problems/racingalphabet

I wrote a first version like that:

... and that kept giving me wrong answers.
So I threw a stealing eye on solutions online and I noticed that they wrote their code differently (obviously). Instead of adding +1 for the pick up, they add the length of the aphorism at the end of the code!
The last line is:
print(total_distance / 15) + len(aphorism).

The correct code is therefore:

I cannot grasp why I cannot add +1 second for pick up between every letter!     So confusing!

Marshal
Posts: 65814
250
• 1
Well, you have found one shortcut, that you add the complete length to your total, not 1 per letter. Now write down on paper how you would calculate the times. Start with finding the time to go from A to B then work out how to get to other letters e.g. M N O including space and '.
I presume your players have to run round the circle, so there are only two possible routes between successive letters, clockwise and anticlockwise.

Now make sure your variable names are clear. Use comments to explain. Also explain why 2 appears twice in line 11. Don't write distance_a/b in lines 16/17, write placesRight and placesLeft or similar. That isn't a distance. Test your minimal distance method to see that it returns the same result if you go from D to J as it does from J to D.

lowercase baba
Posts: 12766
51
Your minimal_distance method is confused.  If it is trying to calculate the DISTANCE, why would you add one SECOND?

What is the result of "13 feet + 1 second"?

Campbell Ritchie
Marshal
Posts: 65814
250
I think the confusion in that method is different from that, Fred.

D.J. Quavern
Ranch Foreman
Posts: 266
12
Oopsie, I am so used to 2*pi*r for circumference that I forgot that it was also 2*diameter...

I understand the code and I am au clair with my variables, the only part of the solution I borrowed is the last line, instead of +1....

Campbell Ritchie
Marshal
Posts: 65814
250
If you go from D to J, as I said earlier, what does your minimal_distance() method return? Now, what happens if you go from J to D? What would happn if you go from Y to D?

fred rosenberger
lowercase baba
Posts: 12766
51

Campbell Ritchie wrote:I think the confusion in that method is different from that, Fred.

i'm not saying mine is the only problem with that method...

But I stand by my statement that it makes no sense to add one second to a distance of 13 feet.

Campbell Ritchie
Marshal
Posts: 65814
250
It's always been correct to add apples and oranges and get bananas The addition of 1″ is only to be found in the earlier, rejected, version of the method.

D.J. Quavern
Ranch Foreman
Posts: 266
12

Campbell Ritchie wrote:If you go from D to J, as I said earlier, what does your minimal_distance() method return? Now, what happens if you go from J to D? What would happn if you go from Y to D?

I tried to make a circular list. If we say D has index 3 and J has index 9, D to J will return 6. 6 mod 28 = 6
J to D will be - 6, modulo 28 will be 22.
The shortest path will be from D to J. That is multiplied by the distance between 2 letters.

In my first attempt I assumed that since the player could run back and forth, it should be always the same distance.

That did not work, that's why I looked up how to do a circular list.

But talking about oranges and bananas... why the +1 does at the end in the distance method does not work?

D.J. Quavern
Ranch Foreman
Posts: 266
12

Campbell Ritchie wrote:Well, you have found one shortcut, that you add the complete length to your total, not 1 per letter.

This is exactly what I cannot understand. It says in the text that the player need one second for pick up. Why can't I add one second for pick up after I calculated the shortest distance between 2 letters?

Campbell Ritchie
Marshal
Posts: 65814
250
• 1: I don't think your method is correctly calculating the shorter distance between pickups. Did you try D→J, J→D and Y→D?
• 2: As Fred said yesterday, you need to add 1″ to the time. What you did in the older version of the method is add 1 foot to the distance.
•
D.J. Quavern
Ranch Foreman
Posts: 266
12

Campbell Ritchie wrote:

• 1: I don't think your method is correctly calculating the shorter distance between pickups. Did you try D→J, J→D and Y→D?
• 2: As Fred said yesterday, you need to add 1″ to the time. What you did in the older version of the method is add 1 foot to the distance.

• 1. Please explain what you mean: the judge gives me a pass on the tests. Can you show me a correct method?

2. oooh. Ok, I was really confused! I don't know why I did that retrospectively...

Campbell Ritchie
Marshal
Posts: 65814
250
That was my mistake; I missed that you were using abs. Sorry about that.

fred rosenberger
lowercase baba
Posts: 12766
51

D.J. Quavern wrote:2. oooh. Ok, I was really confused! I don't know why I did that retrospectively...

Just to emphasize the point...

a method should do ONE thing, and something simple, at that.  Your method was named fairly well as "minimal_distance".  That should clue you in that it should have NOTHING to do with TIME or seconds.

I've moved on from doing much programming so I don't know if this still holds, but they used to say methods should be named like verbs - i.e. "get_minimal_distance".  then when you are reading the code that calls it, it sort of flows better...You could also combine lines 26 and 27 (in your original post/first code):

could be simplified to

Marshal
Posts: 7181
492

alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ '")

Why is your alphabet so weird? What are those 2 right after the Z?

Liutauras Vilda
Marshal
Posts: 7181
492
English alphabet has 26 letters. Everywhere in your calculations you use 28. So again, the talk is about those 2 characters which follow after Z.

D.J. Quavern
Ranch Foreman
Posts: 266
12

Liutauras Vilda wrote:English alphabet has 26 letters. Everywhere in your calculations you use 28. So again, the talk is about those 2 characters which follow after Z.

Hi,
Please read first the problem description. The Kattis-server is on maintenance today, but you will understand if you read it.
https://open.kattis.com/problems/racingalphabet

Liutauras Vilda
Marshal
Posts: 7181
492

D.J. Quavern wrote:Please read first the problem description. The Kattis-server is on maintenance today, but you will understand if you read it.

Tried before posted, but indeed it didn't work.

However, when I see variable name "aphabet" and see there are more than I'd expect, I'd wish to see a-well written comment WHY is it so right above it, OR some other approach to problem to achieve whatever there is in that Kattis-server, so the code wouldn't be puzzling.

As you see server is inaccessible and anybody who didn't read the description days ago when server worked, couldn't understand why the decisions are so, hence couldn't help.

Liutauras Vilda
Marshal
Posts: 7181
492

Earlier, I wrote:OR some other approach to problem to achieve whatever there is in that Kattis-server, so the code wouldn't be puzzling.

An example.

or

Campbell Ritchie
Marshal
Posts: 65814
250
I shall agree with DJ; the total gamut of symbols for a particular grammar is called its alphabet, so it is appropriate for this exercise to have a 28‑symbol alphabet, with the space and apostrophe added.

Liutauras Vilda
Marshal
Posts: 7181
492
Since server started working again, I can go and read, so I might agree too.

 Popeye has his spinach. I have this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton