0% found this document useful (0 votes)
3 views9 pages

Test Cases Overview in Both Java and Python

The document provides various cases for reading integers in Python and Java, showcasing different input formats such as space-separated, line-separated, comma-separated, and lists within brackets. Each case includes example input and corresponding code snippets for both programming languages. The cases also cover scenarios involving target values and extracting integers from mixed strings.

Uploaded by

deleepreddy2006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Test Cases Overview in Both Java and Python

The document provides various cases for reading integers in Python and Java, showcasing different input formats such as space-separated, line-separated, comma-separated, and lists within brackets. Each case includes example input and corresponding code snippets for both programming languages. The cases also cover scenarios involving target values and extracting integers from mixed strings.

Uploaded by

deleepreddy2006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Case 1: Space-separated integers

Example Input

23 34

Python Code

n, m = map(int, input().split())

Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int m = [Link]();
}
}

Case 2: Integers on separate lines


Example Input

23
34

Python Code

n = int(input())
m = int(input())
Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int m = [Link]();
}
}

Case 3: List of integers on one line


Example Input

23 34 55 67 78

Python Code

l = list(map(int, input().split()))

Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] parts = [Link]().split(" ");
List<Integer> l = new ArrayList<>();
for (String p : parts) {
[Link]([Link](p));
}
}
}

Case 4: Multiple integers entered line by line


Example Input

4
12
34
56
46

Python Code

n = int(input())
l = []
for i in range(n):
[Link](int(input()))

Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
List<Integer> l = new ArrayList<>();
for (int i = 0; i < n; i++) {
[Link]([Link]());
}
}
}

Case 5: Comma-separated integers


Example Input

23,34

Python Code

n, m = map(int, input().split(","))

Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] parts = [Link]().split(",");
int n = [Link](parts[0].trim());
int m = [Link](parts[1].trim());
}
}

Case 6: List inside brackets and a separate value


Example Input

[12,23,34,45,56]

Python Code

l = list(map(int, input().strip("[]").split(",")))
Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String line = [Link]().trim();
line = [Link]("[", "").replace("]", "");
String[] parts = [Link](",");
List<Integer> l = new ArrayList<>();
for (String p : parts) {
[Link]([Link]([Link]()));
}
}
}

Case 7: Two inputs (list + integer)


Example Input

12 13 14 15
34

Python Code

l = list(map(int, input().split()))
k = int(input())

Java Code

import [Link].*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] parts = [Link]().split(" ");
List<Integer> l = new ArrayList<>();
for (String p : parts) {
[Link]([Link](p));
}
int k = [Link]();
}
}

Case 8: First number indicates size, followed by array


Example Input

5 12 23 34 45 56

Python Code

l = list(map(int, input().split()))
size = l[0]
arr = l[1:]

Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] parts = [Link]().split(" ");
int size = [Link](parts[0]);
List<Integer> arr = new ArrayList<>();
for (int i = 1; i < [Link]; i++) {
[Link]([Link](parts[i]));
}
}
}

Case 9: Last value is a target, rest are array elements


Example Input

12 23 34 56 67 90

Python Code

l = list(map(int, input().split()))
target = l[-1]
arr = l[:-1]

Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String[] parts = [Link]().split(" ");
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < [Link] - 1; i++) {
[Link]([Link](parts[i]));
}
int target = [Link](parts[[Link] - 1]);
}
}
Case 10: Extract integers (including negatives) from
mixed string
Example Input

-2 -3 4 -89 43 -56 34

Python Code

l=input()
s = []
temp = ""
for i in l:
if [Link]() or (temp == "" and i == "-"):
temp += i
else:
if temp != "" and temp != "-":
[Link](int(temp))
temp = ""
if temp != "" and temp != "-":
[Link](int(temp))

Java Code

import [Link].*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String line = [Link]();
List<Integer> s = new ArrayList<>();
String temp = "";

for (int i = 0; i < [Link](); i++) {


char ch = [Link](i);
if ([Link](ch) || ([Link]() && ch == '-')) {
temp += ch;
} else {
if (![Link]() && ![Link]("-")) {
[Link]([Link](temp));
}
temp = "";
}
}
if (![Link]() && ![Link]("-")) {
[Link]([Link](temp));
}

[Link](s);
}
}

You might also like