r/dailyprogrammer • u/G33kDude 1 1 • May 30 '16
[2016-05-30] Challenge #269 [Easy] BASIC Formatting
Description
It's the year 2095. In an interesting turn of events, it was decided 50 years ago that BASIC is by far the universally best language. You work for a company by the name of SpaceCorp, who has recently merged with a much smaller company MixCo. While SpaceCorp has rigorous formatting guidelines, exactly 4 space per level of indentation, MixCo developers seem to format however they please at the moment. Your job is to bring MixCo's development projects up to standards.
Input Description
You'll be given a number N, representing the number of lines of BASIC code. Following that will be a line containing the text to use for indentation, which will be ···· for the purposes of visibility. Finally, there will be N lines of pseudocode mixing indentation types (space and tab, represented by · and » for visibility) that need to be reindented.
Blocks are denoted by IF and ENDIF, as well as FOR and NEXT.
Output Description
You should output the BASIC indented by SpaceCorp guidelines.
Challenge Input
12
····
VAR I
·FOR I=1 TO 31
»»»»IF !(I MOD 3) THEN
··PRINT "FIZZ"
··»»ENDIF
»»»»····IF !(I MOD 5) THEN
»»»»··PRINT "BUZZ"
··»»»»»»ENDIF
»»»»IF (I MOD 3) && (I MOD 5) THEN
······PRINT "FIZZBUZZ"
··»»ENDIF
»»»»·NEXTChallenge Output
VAR I
FOR I=1 TO 31
····IF !(I MOD 3) THEN
········PRINT "FIZZ"
····ENDIF
····IF !(I MOD 5) THEN
········PRINT "BUZZ"
····ENDIF
····IF (I MOD 3) && (I MOD 5) THEN
········PRINT "FIZZBUZZ"
····ENDIF
NEXTBonus
Give an error code for mismatched or missing statements. For example, this has a missing ENDIF:
FOR I=0 TO 10
····IF I MOD 2 THEN
········PRINT I
NEXTThis has a missing ENDIF and a missing NEXT:
FOR I=0 TO 10
····IF I MOD 2 THEN
········PRINT IThis has an ENDIF with no IF and a FOR with no NEXT:
FOR I=0 TO 10
····PRINT I
ENDIFThis has an extra ENDIF:
FOR I=0 TO 10
····PRINT I
NEXT
ENDIFFinally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
Edit: Added an extra bonus input
6
u/G33kDude 1 1 May 30 '16 edited May 30 '16
Solution in Py3, does not implement bonus.
+/u/CompileBot Python3
Edit: Now with bonus
2
u/ih8uh8me May 30 '16
I think there is a little mistake on the bonus question. You mean the first one is missing ENDLIF instead of ELSEIF ?
2
u/FlammableMarshmallow May 31 '16 edited Jun 01 '16
Python 3
Nothing much to say.
EDIT: Improved the code thanks to /u/G33kDude!
2
u/kallekro Jun 01 '16 edited Jun 01 '16
Another C# solution, no bonus. (Mostly because I wanted to try CompileBot)
+/u/CompileBot C#
Input:
2
u/bitx0r Jun 03 '16
AutoHotkey - No bonus
Results:
5
u/jnd-au 0 1 May 30 '16
Scala with an extensible list of keywords and human-friendly bonus outputs:
Unexpected NEXT at line 4: Expected ENDIF
Unexpected EOF at line 3: Expected ENDIF
Unexpected ENDIF at line 3: Expected NEXT
It’s a function that you call like
indent(input, " "):5
u/Starbeamrainbowlabs May 31 '16 edited May 31 '16
I'm a student at University who recently been learning C++. I thought this challenge to be suitably simple that I could probably give it a good go - especially considering I have an exam on this stuff exam this afternoon.
Constructive criticism is welcomed. Please kind kind considering I'm a student who's rather new to this (horrible) C++ business (I prefer C#!)
Not that this entry doesn't implement the bonus challenge. Also note that I had to substiture the given replacement characters with a full stop and a '>' since C++ hated them.
4
u/Godspiral 3 3 May 30 '16 edited May 31 '16
3
u/X-L May 30 '16
JAVA
Output
3
u/ShaharNJIT 0 1 May 31 '16
Ruby, essentially 1-liner:
+/u/CompileBot Ruby
3
u/[deleted] May 31 '16
Forth
<stdin>
<stdout>
<stdin>
<stdout>
<stdin>
<stdout>
<stdin>
<stdout>
<stdin>
<stdout>
2
u/niandra3 May 30 '16 edited May 30 '16
Solution in Python3 with bonus. It will log errors if blocks aren't closed correctly, but it will also try to print how the code should be formatted, regardless of errors.
Edit: Inspired by /u/jnd-au I added support for easily adding new block keywords (in addition to IF/ENDIF, FOR/NEXT). Also added error checking so if the code is really bad it won't end up with negative indent levels.
+/u/CompileBot Python3
2
u/Excappe May 30 '16 edited May 30 '16
Python3 with bonus, my first submission ever... Probably not the most elegant, but here goes:
+/u/CompileBot Python3
2
u/ih8uh8me May 30 '16
Python 3.5
No bonus yet !
2
u/timvw74 May 30 '16
And a bit of messy c#
2
u/hutsboR 3 0 May 31 '16
Elixir: Sorry.
2
u/voice-of-hermes May 31 '16 edited May 31 '16
With bonus.
EDIT: I used spaces and tabs in the actual input files, replacing the stand-in characters in the OP before testing.
2
u/WillingCodeAcolyte May 31 '16 edited May 31 '16
C# without the bonus (...yet) I get the feeling this is probably inefficient, and maybe too verbose? Comments and feedback welcome!
2
u/vishal_mum May 31 '16
Rust solution. The input was in a file
Output
2
u/FrankRuben27 0 1 May 31 '16
Typed Racket (incl. bonus, not using Racket parser tools):
2
u/a_Happy_Tiny_Bunny May 31 '16 edited May 31 '16
Haskell
Writing the
formatin a style similar to that of the canonical implementation of the Fibonacci sequence was fun.I'm pretty sure Windows or GHC for Windows messed up something about the encoding, so I replaced
·and»with>and*respectively. The challenge input is then:2
u/aMoosing May 31 '16
Groovy solution w/o bonus
2
u/fbWright May 31 '16
Python3, with bonus
2
u/beam May 31 '16
Shell. No bonus.
2
u/dailyBrogrammer May 31 '16
Scheme with all bonuses. This one feels pretty ugly to me but it appears to get the job done. I very much welcome criticism and improvements! I placed the exact text from the prompt into files with the exception of the "bad" examples. I added the total lines and the formatting information for instance bad1.bas looks like:
On to my solution!
** Main Problem**
** Bonuses **
2
u/antonmry Jun 01 '16
Golang with bonus: https://play.golang.org/p/fvtR4wwdHU
2
u/draegtun Jun 01 '16 edited Jun 01 '16
Rebol (with bonus)
NB. Tested in Rebol 3
2
u/Albaforia Jun 01 '16
C++11 with the bonus.
I haven't coded in C++ in a while, and I also have quite a few comments. Suggestions would be appreciated!
Input it taken in via file. Compile it first using g++, and place the filename as the first argument:
2
u/Scroph 0 0 Jun 01 '16 edited Jun 01 '16
A bit late to the party. C++ solution with bonus :
2
u/The_Jare Jun 02 '16
Rust, with bonuses and easily extensible to more keywords
2
u/LordJackass Jun 03 '16
C++ solution with bonus.
Also I replaced those '·' characters with plain periods '.' and deleted the '»' characters because I wasnt sure what was their purpose.
2
u/pvejunky12 Jun 04 '16
Solution in Java (with Bonus):
2
u/keeslinp Jun 05 '16
Getting back into the swing of things with Ruby was a little harder than I was expecting :). Been a good minute since I've written in that language. I'm sure there's a more golf way to do it but I'll try that later.
2
u/sanfly Jun 06 '16
Java with bonus
I'm quite new to Java, teaching myself, would appreciate constructive feedback
EDIT: just to say I saved the test text as txt files for input
2
u/[deleted] Jun 11 '16
Python 3
Substituted the space character for "-" and the tab character for ">" for ease of typing.
Not the prettiest solution out there but it works.
2
u/gastropner Jul 19 '16
C++, no bonus:
1
u/PetarMihalj May 30 '16
Solution with bonus in Python3, took some of "G33kDude"s code.
1
u/mprosk Jul 15 '16
First submission to this subreddit!! Solution using Python 3 Implements the bonus
1
u/[deleted] Sep 04 '16 edited Sep 04 '16
First time poster. Please be kind.
+/u/CompileBot ruby
0
u/[deleted] May 31 '16
[deleted] — view deleted comment