Jump to content

counting characters without "if" statements

AntiHero

So here's the question "Write a program that reads two words representing passwords from the Java console and outputs the number of characters in the smaller of the two. Example: the password word enter is "open sesame", the output would be 4 the length of the shorter word". the catch, not allowed to use and "if" statement.

Link to comment
Share on other sites

Link to post
Share on other sites

There is the tertiary operator, and there's a method you can call that'll give you the larger of two (or more) values passed in. I'll leave you to the specifics.

Wife's build: Amethyst - Ryzen 9 3900X, 32GB G.Skill Ripjaws V DDR4-3200, ASUS Prime X570-P, EVGA RTX 3080 FTW3 12GB, Corsair Obsidian 750D, Corsair RM1000 (yellow label)

My build: Mira - Ryzen 7 3700X, 32GB EVGA DDR4-3200, ASUS Prime X470-PRO, EVGA RTX 3070 XC3, beQuiet Dark Base 900, EVGA 1000 G6

Link to comment
Share on other sites

Link to post
Share on other sites

Technically, a ternary operator (not tertiary ?) is an implicit "if" statement, so I'm not sure whether it would also violate the rules. The code should not need one anyway, if the input is guaranteed to have exactly two words.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, Dat Guy said:

 The code should not need one anyway, if the input is guaranteed to have exactly two words.

I doesn't need to be guaranteed to have only 2 words, you can do it in 1 line without IF statement with any length text.

- Split by space

- select count

- order by count

- pick first

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/22/2019 at 6:19 PM, Franck said:

I doesn't need to be guaranteed to have only 2 words, you can do it in 1 line without IF statement with any length text.

- Split by space

- select count

- order by count

- pick first

I feel like this is cheating.
order by count, if use the inbuilt api will use if statements.
even if use the stream api.

even the split function uses if statements; probably somechar == nextchar or something like that
but I believe this problem is pretty to hard to solve without an if statement.
but you can use mathematics, but you might need an if statement, havent thought about it too much

Edited by Aqeel Hashim
wanted to mention that the split function has if statements
Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Aqeel Hashim said:

I feel like this is cheating.
order by count, if use the inbuilt api will use if statements.
even if use the stream api.

even the split function uses if statements; probably somechar == nextchar or something like that
but I believe this problem is pretty to hard to solve without an if statement.
but you can use mathematics, but you might need an if statement, havent thought about it too much

If you get nit picky as to inbuilt If statement then you won't be able to do it. You are nearly certain to have an if statement somewhere no matter what you do. A loop has a conditional if.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

Math.min(a, b).

 

Internally it uses the Ternary Operator when using ints.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×