7 kyu

Broken sequence

986 of 4,393suuuzi

Description:

You receive some random elements as a space-delimited string. Check if the elements are part of an ascending sequence of integers starting with 1, with an increment of 1 (e.g. 1, 2, 3, 4).

Return:

  • 0 if the elements can form such a sequence, and no number is missing ("not broken", e.g. "1 2 4 3")
  • 1 if there are any non-numeric elements in the input ("invalid", e.g. "1 2 a")
  • n if the elements are part of such a sequence, but some numbers are missing, and n is the lowest of them ("broken", e.g. "1 2 4" or "1 5")

Examples

"1 2 3 4"  ==>  return 0, because the sequence is complete

"1 2 4 3"  ==>  return 0, because the sequence is complete (order doesn't matter)

"2 1 3 a"  ==>  return 1, because it contains a non numerical character

"1 3 2 5"  ==>  return 4, because 4 is missing from the sequence

"1 5"      ==>  return 2, because the sequence is missing 2, 3, 4 and 2 is the lowest
Strings
Fundamentals

More By Author:

Check out these other kata created by suuuzi

Stats:

CreatedMar 25, 2015
PublishedMar 25, 2015
Warriors Trained15351
Total Skips3137
Total Code Submissions50183
Total Times Completed4393
Java Completions986
Python Completions1354
JavaScript Completions1523
Ruby Completions607
Total Stars250
% of votes with a positive feedback rating87% of 892
Total "Very Satisfied" Votes697
Total "Somewhat Satisfied" Votes151
Total "Not Satisfied" Votes44
Total Rank Assessments230
Average Assessed Rank
6 kyu
Highest Assessed Rank
2 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • suuuzi Avatar
  • jhoffner Avatar
  • zebulan Avatar
  • GiacomoSorbi Avatar
  • anter69 Avatar
  • PG1 Avatar
  • ejini战神 Avatar
Ad