This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Coding challenge: Three 3's  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stumbled in an another issue.  A bit feared to put here because I think there are too many if - else statement.
The problem is that:
Task Given an array of ints, print 'false' if the value '3' appears in the array exactly 3 times and no '3's are next to each other. Otherwise print 'true'.  Input Format The first line contains a single integer, n, denoting the size of the array. Each line i of the n subsequent lines contains a single integer denoting the value of the array at position i.  Output Format Simply the word 'true' or 'false'.


This page put me back these values:
Input                              Expected Output           Output
4 3 1 3 3                              true                        true
5 3 1 3 1 3                      false                        true
5 3 4 3 3 4                      true                        true
3 3 3 3                              true                        false
6 3 1 3 3 4 6                      true                        true
7 1 3 4 5 3 2 3                      false                        true
7 3 1 3 2 3 4 5                      false                        false
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there should be a logic-bug. What do you think?
 
Sheriff
Posts: 5688
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would break this down logically on paper (or whatever you use for writing text).

Think about how you would solve this as a human, and only then start coding.

So the challenge can be brokening into two tasks: 1) no consecutive 3's, and 2) a total of exactly three 3's.  The second tack is the easiest: sum the number of 3's.  You seem to be doing that.  Then think of how you would determine if two 3's are next to each other.

Got all that?  Okay, you can start coding again.  I would save your first, or start a new isIt() method.
 
Saloon Keeper
Posts: 2146
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Barbara Fischer wrote:... print 'false' if the value '3' appears in the array exactly 3 times and no '3's are next to each other. Otherwise print 'true'...



I hate it when problem statements have two negatives in the same sentence/context. I don't find it intuitive at all.
Have a look at this :
"Push this button to send email"
vs
"Don't push this button if you don't want to send an email"

I typically re-word such problem statements to suit my intuition:
If either input contains "33" or input does not contain exactly 3 "3"s, then print "true". Else print "false"

I hope this helps you
 
Sheriff
Posts: 12992
217
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:
I typically re-word such problem statements to suit my intuition:
If either input contains "33" or input does not contain exactly 3 "3"s, then print "true". Else print "false"


I don't think that matches the original requirements though.

The original requirements can basically be summarized as:

That's actually how I would decompose this problem. That big loop is a monster that is difficult to understand, debug, and prove that it's correct. Breaking it down like this will make all that easier.
 
salvin francis
Saloon Keeper
Posts: 2146
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:...

Is incorrect, but you have proved my point.. two negatives in sentence confuse us all.

Problem statement:
Given an array of ints, print 'false' if the value '3' appears in the array exactly 3 times and no '3's are next to each other. Otherwise print 'true'.

Maybe if you called your methods : hasAnyConsecutive3s and meetsFalseRequirements I would agree with you

Also your check would be:
 
Junilu Lacar
Sheriff
Posts: 12992
217
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, although I would rename to be able to write:
 
salvin francis
Saloon Keeper
Posts: 2146
80
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:...


Agreed !

Although I suggested it, I dislike "meetsFalseRequirements". I am having a hard time thinking of a method that succeeds and yet returns false
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!