#Beginner level snippet redo, from py to Java

90 messages · Page 1 of 1 (latest)

modern glen
#

So, I made this small snippet, but can't get to to work on Java


# return true if a list is made of lists and integers only, it's recursive 

def checkIfValid(obj):
  for i in obj: 
    if type(i) == list: 
      return checkIfValid(i)
    elif type(i) == int : 
      pass 
    else: return False 
    return True 

a = [1, 2, 3, [1,2, 3, [2, 2, "a"]]] 
b = [1, 2, 3, [1,2, 3, [2, 2, []]]]

if (checkIfValid(a)): print("a")
if (checkIfValid(b)): print("b")

daring badgeBOT
#

⌛ This post has been reserved for your question.

Hey @modern glen! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

coarse fjord
#

So what's the Java code you currently have?

#

Java is type-safe and you typically don't have lists containing both ints and strings or whatever

#

like you'd use List<Integer> and then it can't contain any String unless you are using unchecked operations somewhere

modern glen
#

package NetBeans4Tests;
 
import java.util.Scanner;
 
public class App {
    public static void main(String[] args) {
 
        Object[] a = {1, 2, 3};
        Object[] b = {1, 2, "3"};
        Object[] aa = {1, 2, a};
        Object[] bb = {1, 2, b};
 
        if(checkIfValid(a)){
            System.out.println("a");
        }
        if(checkIfValid(b)){
            System.out.println("a");
        }
        if(checkIfValid(aa)){
            System.out.println("a");
        }
        if(checkIfValid(bb)){
            System.out.println("a");
        }
    }
 
 
    public static boolean checkIfValid(Object[] obj){
        for(Object i: obj){
            if (i instanceof Object[]){
                return checkIfValid(obj);
            } else if (i instanceof Integer) {
                // pass
            } else {
                System.out.println("false because" + i);
            return false;
            }
        }
        return true;
 
    }
}
coarse fjord
#

oh you are using arrays and not lists?

#

ok

modern glen
coarse fjord
#

in the return checkIfValid(obj);, you'd need to use (Object[]) i

#

since you'd essentially check the whole array again instead of the single object

modern glen
coarse fjord
#

but you could also use instanceof patternmatching

#
    public static boolean checkIfValid(Object[] obj){
        for(Object i: obj){
            if (i instanceof Object[] arr){
                return checkIfValid(arr);
            } else if (i instanceof Integer) {
                // pass
            } else {
                System.out.println("false because" + i);
                return false;
            }
        }
        return true;
 
    }
naive idol
#

couldn't you just ignore the integer case enitrely and just !(i instanceof Integer)

#

in python as well

modern glen
coarse fjord
#

it does two things

#

at first, it does the instanceof check

#

and if it succeeds, it does a cast

naive idol
coarse fjord
#
if (i instanceof Object[] arr){
  return checkIfValid(arr);
}

is equivalent to

if (i instanceof Object[]){
  Object[] arr = (Object[]) i;
  return checkIfValid(arr);
}
modern glen
coarse fjord
naive idol
#

this would be switch pattern matching

switch (i) {
  case Object[] arr:
    return checkIfValid(arr);
  case Integer i:
    break;
  default:
    return false;
}
naive idol
#

yeah but it's also a valid solution on its own so.. why not show it

#

sidenote though; why return checkIfValid(arr) directly?
couldn't you have an array in the middle that is itself valid, but then have invalid elements after the array?

coarse fjord
# modern glen _wat_

in your code, you used return checkIfValid(obj);.
This would check the whole array again in each iteration which would lead to infinite recursion

modern glen
coarse fjord
naive idol
#

should be something like

if (!checkIfValid(arr)) {
  return false;
}
``````py
if not check_if_valid(i):
  return False
modern glen
naive idol
coarse fjord
modern glen
coarse fjord
#

if(i instanceof Object[] arr) means "take i and check whether it is an Object[]. If it is, create a new variable arr holding the Object[]"

naive idol
#

this is special syntax

#

instanceof checks behave differently to type equality checks

#

in python it'd be instanceof(obj, type) iirc

coarse fjord
modern glen
daring badgeBOT
naive idol
modern glen
naive idol
#

your current algorithm (both py and java) return immediately upon encountering an array, not checking any remaining elements

#

it's like if you had a return true; in the if type(i) == int branch

modern glen
naive idol
#

it's not an idea.. your program has a bug

coarse fjord
#

although not even that

naive idol
#

it isn't

coarse fjord
#

ah right that's what you mean

#

yeah

modern glen
naive idol
#

i mean there is an indentation bug in the python code, but what im talking about is a bug in the algorithm

coarse fjord
#

so if we go back to the Python code:

def checkIfValid(obj):
  for i in obj: 
    if type(i) == list: 
      return checkIfValid(i)
    elif type(i) == int : 
      pass 
    else: return False 
    return True 

you have return checkIfValid(i).

Now, if you have a list like [[1],"Hi"]

modern glen
#

Formatting got lost in the way I've to copy and paste, please ignore my formatting kekw

naive idol
#

so... you acknowledge that you have a bug, but fixing the bug isn't logical because you aren't trying to do it?

coarse fjord
#

but you are ignoring further elements

#

so [[1],"Hi"] would be true with your Python code

naive idol
coarse fjord
#

and you also have that issue in your Java code

modern glen
coarse fjord
naive idol
#

it's.. the same explanation though

modern glen
naive idol
modern glen
#

Anyway, thank you both

daring badgeBOT
# modern glen Anyway, thank you both

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

coarse fjord
naive idol
#

not even a postponement, just straight up, "bug not relevant ever"?

#

im so confused by your line of thinking

coarse fjord
#

oh and I didn't see some of your messages

modern glen
naive idol
#

no it absolutely is

modern glen
#

@coarse fjord thank you a lot for the instanceof casting thingy!

naive idol
#

i have no idea what you're talking about

coarse fjord
naive idol
#

translating code is an opportunity to restructure old code or fix bugs, and to introduce new ones

modern glen
naive idol
#

this isn't mean for production of anything like that.
not relevant