H
H
hellcaster2020-02-02 12:35:11
Python
hellcaster, 2020-02-02 12:35:11

Is the problem in the tests or in the code?

Hey! There are 2 identical (or almost) programs in Python and Java.

Briefly what it does:
There is a robot that needs to cut the cake into N (2 <= N <= 100) equal parts. The first cut is drawn from the center to
K (0.00 <= K <= 359.99) degrees (perfect circle cake). You need to output where the last cut will be.
Enter two numbers N, K separated by a space.
Output one number: answer with 1 number after the dot.

Example:
Input: 2 300.00 Output: 120.0
Input: 100 0.01 Output: 356.4

Python

from math import floor

count, start = input().split()
count, start = float(count), float(start)

onePiece = 360 / count
lastPiece = abs(start + ((count - 1) * onePiece))

if lastPiece > 360:
  lastPiece = lastPiece - (floor(lastPiece / 360) * 360)

print(f"{lastPiece:.1f}")


Java

import java.util.Scanner;
import java.text.DecimalFormat;

public class MyClass {
    public static void main(String args[]) {
      Scanner scanner = new Scanner(System.in);
      String s = scanner.nextLine();
      
      String[] tokens = s.split(" ");
      Double count = Double.valueOf(tokens[0]);
      Double start = Double.valueOf(tokens[1]);

      Double onePiece = 360 / count;
      Double lastPiece = Math.abs(start + ((count - 1) * onePiece));

      if (lastPiece > 360) {
        lastPiece = lastPiece - (Math.floor(lastPiece / 360) * 360);
      }
      
      String res = new DecimalFormat("#0.0").format(lastPiece);
      
      System.out.println(res);
    }
}



2 programs are the same (first written in Python, then had to be rewritten in Java). When testing in Python, they showed me Run-time error 1 test, but in Java, all tests were successful.
Tell me, please, is there a problem in the program or did the tests differ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2020-02-02
@o5a

It's better to use %

def last_cut(N, K):
    circle = 360
    part = circle/N
    result = (part*(N-1)+K)%circle
    return result

A
Andy_U, 2020-02-02
@Andy_U

Why is your count variable not 'int' in both cases?
Why is abs needed when calculating 'lastPiece'?
What for handles to calculate the module in python, if there is an operator '%' ?
It's also possible that the 360.0 response returned by your code, for example with the input string "2 180.0", could be interpreted as invalid - should 0.0 be printed?
Those. everything is simpler than you piled up (although I don’t see the source of RuntimeError in your code):

if __name__ == '__main__':
    count, start = input('input count and start: ').split()
    count, start = int(count), float(start)
    last_piece = (start + (count - 1) * 360 / count) % 360
    print(f"{last_piece:.1f}")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question