• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need feedback on code

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given a number (Integer) where each digit is in range
1..7 according to the day of week (1 - Monday, 2 -
Tuesday... 7 -Sunday).
E.g. 126 - means Monday, Tuesday and Saturday.
Convert this number to the string, separating each digit
with ‘,’ for non consecutive sequence days, or ‘-’ for
consecutive sequence days.
Examples:
123 -> ‘1-3’
135 -> ‘1,3,5’
125 -> ‘1-2,5’
12367 -> ‘1-3,6-7’
134567 -> ‘1,3-7’
At least one digit <1 or >7 -> error

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you compile and execute the code?
 
Oleg Kamuz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:What happens when you compile and execute the code?



--- Stream Solution ---
[1, 2, 4, 5, 6] => 1-2,4-6
[1, 2, 3] => 1-3
[1, 3, 5] => 1,3,5
[1, 2, 5] => 1-2,5
[1, 2, 3, 6, 7] => 1-3,6-7
[1, 3, 4, 5, 6, 7] => 1,3-7
error - single element!
--- Stream Days Names Solution ---
[1, 2, 4, 5, 6] => Monday-Tuesday,Thursday-Saturday
[1, 2, 3] => Monday-Wednesday
[1, 3, 5] => Monday,Wednesday,Friday
[1, 2, 5] => Monday-Tuesday,Friday
[1, 2, 3, 6, 7] => Monday-Wednesday,Saturday-Sunday
[1, 3, 4, 5, 6, 7] => Monday,Wednesday-Sunday
error - single element!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oleg Kamuz wrote:
At least one digit <1 or >7 -> error


I don't agree with your interpretation of the error condition.

I think this is the intent:
 
Oleg Kamuz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
I don't agree with your interpretation of the error condition.



I agree, my mistake. Thank you.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is a very roundabout way. What's wrong with if (daysNumber.size() == 1) -- Using streams is one thing but using them to do something that could be done much more simply is probably not a good choice.
reply
    Bookmark Topic Watch Topic
  • New Topic