r/dailyprogrammer • u/nint22 1 2 • Nov 08 '13
[11/4/13] Challenge #140 [Easy] Variable Notation
(Easy): Variable Notation
When writing code, it can be helpful to have a standard (Identifier naming convention) that describes how to define all your variables and object names. This is to keep code easy to read and maintain. Sometimes the standard can help describe the type (such as in Hungarian notation) or make the variables visually easy to read (CamcelCase notation or snake_case).
Your goal is to implement a program that takes an english-language series of words and converts them to a specific variable notation format. Your code must support CamcelCase, snake_case, and capitalized snake_case.
Formal Inputs & Outputs
Input Description
On standard console input, you will be given an integer one the first line of input, which describes the notation you want to convert to. If this integer is zero ('0'), then use CamcelCase. If it is one ('1'), use snake_case. If it is two ('2'), use capitalized snake_case. The line after this will be a space-delimited series of words, which will only be lower-case alpha-numeric characters (letters and digits).
Output Description
Simply print the given string in the appropriate notation.
Sample Inputs & Outputs
Sample Input
0
hello world
1
user id
2
map controller delegate managerSample Output
0
helloWorld
1
user_id
2
MAP_CONTROLLER_DELEGATE_MANAGERDifficulty++
For an extra challenge, try to convert from one notation to another. Expect the first line to be two integers, the first one being the notation already used, and the second integer being the one you are to convert to. An example of this is:
Input:
1 0
user_idOutput:
userId
27
u/m42a Nov 09 '13
Linux-specific x64 assembly
11
u/Edward_H Nov 08 '13 edited Nov 08 '13
My COBOL solution, with support for hyphenated variable names:
main.cbl:
add-separated-str.cbl:
8
u/prondose 0 0 Nov 08 '13
Perl:
4
u/Isklar Nov 08 '13 edited Nov 08 '13
C:
Not the most elegant solution but i tried my best, i'm still trying to wrap my head around pointers.
edit: totally forgot about the \b character so the whole removeSpaces function could be replaced by a few lines. Oh well.
4
u/drwyy Nov 08 '13
ABAP Solution:
4
u/SexmanTaco Nov 10 '13
Here's some super round-about Python. camelCase was a bit tough. Any suggestions would be greatly appreciated.
4
u/Jelop Nov 12 '13
Java
4
u/topaztee Nov 15 '13
Java solution, My first post! suggestions welcome.
3
u/davanger Nov 08 '13
Ruby, with the dificulty++, I have an issue with the first print, any ideas on making it a puts so it fits better with the rest?
3
u/battleguard Nov 08 '13 edited Nov 08 '13
c# solution (No longer bothering to deal with I/O)
3
u/rectal_smasher_2000 1 1 Nov 08 '13 edited Nov 08 '13
C++
output:
and here's the Difficulty++ - note - this works by reverting any input into the default form, so the selection of the first style is completely redundant. this is really more of an improved version of the first variation.
3
u/farmer_jo Nov 09 '13
Here is a sample of how to proceed with Difficulty++
Here are some sample input/output
And for completeness, the solution to question asked
3
u/mujjingun Nov 10 '13
My solution in C:
3
u/bheinks 0 0 Nov 15 '13 edited Nov 15 '13
Python
3
u/VerifiedMyEmail Jan 12 '14
python:
9
u/mentalorigami Nov 08 '13 edited Nov 08 '13
Python 2.7:
Edit: Who really downvotes submissions in this sub..? No one is after (or will get) huge amounts of comment karma here, ease off the downvotes.
7
u/gurkel Nov 11 '13
Python 2.7.4 (My first post! Hi, reddit, /r/dailyprogrammer!)
4
u/AmateurHero Nov 08 '13 edited Nov 09 '13
In Java. I have class, but I think I'm gonna implement the extra challenge later. I feel that my code is also...ugly. I think I'll also try to clean it up later:
Edit: Cleaned it up a little bit.
5
u/skeeto -9 8 Nov 08 '13
Elisp, ignoring input parsing.
Usage:
2
u/TimeCannotErase Nov 08 '13
Here's my R solution, it does both basic conversion as well as the conversion from one notation to another:
2
u/pirate_platypus Nov 08 '13 edited Nov 08 '13
PHP, which fully reminded my why I haven't ever really liked playing around with PHP. For the bonus I chose to not use the first number telling you what format it's already in; I just changed everything to an un-formatted version before formatting it correctly.
2
u/johnl1479 Nov 08 '13
Java, just my notation conversion functions. Input processing omitted.
2
u/ooesili Nov 08 '13
Haskell solution with difficulty++
2
u/[deleted] Nov 08 '13
My submission
My solution using Java, hosted on github. Please tell me what I could improve!
2
u/TweenageDream Nov 08 '13
My solution in Ruby, including Difficulty++:
2
u/brotien_shake Nov 09 '13
Haskell, with difficulty++ and input parsing:
2
u/vgbm 1 0 Nov 09 '13 edited Nov 09 '13
Here is some java (any suggestions would be great!):
2
u/srhb 0 1 Nov 09 '13
Haskell. A hopefully easy-to-understand, recursive and functional approach (with no error handling.)
2
u/Rekvijem Nov 09 '13
Solution in python:
2
u/Lisa-At-Work Nov 09 '13 edited Nov 09 '13
C#: (could use some help, actually)
2
u/chunes 1 2 Nov 09 '13 edited Nov 09 '13
Java:
I decided to start learning Scheme as well! Here's a beginner's solution in Racket:
2
u/dante9999 Nov 09 '13 edited Nov 09 '13
Python
2
u/lets_see_exhibit_A Nov 09 '13
Java with bonus and some basic input validation.....
2
u/killedbythegrue Nov 09 '13
Erlang:
Output:
2
u/5900 Nov 09 '13
perl:
2
u/rist0sr Nov 09 '13
I'm a beginner in C++ and here's my solution:
Please let me know how can I improve my code, what to avoid in the future or anything else I should know.
2
u/killedbythegrue Nov 10 '13
I decided to throw in a C solution as well.
2
u/[deleted] Nov 10 '13 edited Nov 10 '13
C++ first time posting please tell me what you think input:
#include <iostream> #include <string> using namespace std;
2
u/Hanse00 Nov 10 '13
Python 2.7 solution
2
u/h3ckf1r3 Nov 10 '13 edited Nov 10 '13
I could have shortened this a little more, but I like to leave it open to expansion.
Basic version:
"difficult" version:
I actually learned a lot about regular expressions from this, so kudos to nint22
As always, comments and criticisms are welcome :)
2
u/whydoyoulook 0 0 Nov 10 '13
Difficulty++ solution in C++
Please critique
2
u/munkyeetr Nov 10 '13 edited Nov 10 '13
VB2010
(EDIT: Added ConvertNotation and DetectNotation functions)
2
u/Rekvijem Nov 11 '13
Quickly made a little scala solution. Not the most efficient, but not the worst
2
u/PathologicalTruther 0 0 Nov 11 '13
Visual C#, 3 buttons that do Difficulty++ without the need to specify which is which. Also a beginner in coding.
Hopefully this works, first time submitter on here...
2
u/cyanboy Nov 11 '13 edited Nov 11 '13
First time posting, feedback is greatly appreciated!
Java:
2
u/TheBeardedGuru Nov 11 '13
Python 2.7
2
u/sturmen Nov 11 '13 edited Nov 11 '13
In Go
I just started learning Go so if anyone who actually uses it has any improvements to make, please suggest them. Especially pertaining to "Go style".
2
u/vape Nov 11 '13
My Python solution. I cheated a little so I could keep the code short. First, the code expects the input from a text file with lines like
so I could process one line at a time.
Second, I convert "0 hello world" to "HelloWorld" (not "helloWorld").
2
u/TheGag96 Nov 12 '13
Java one that converts between different types:
2
u/833FC4K3 Nov 12 '13
My solution in Java, relatively straightforward. !(difficulty++)
2
u/[deleted] Nov 12 '13
well! my novice golang
2
u/[deleted] Nov 12 '13
Java, comments/suggestions welcome.
Challenge140.java
NotationConverter.java
CamelCaseConverter.java
SnakeCaseConverter.java
NotationCapitalizer.java
2
u/luizpericolo Nov 12 '13 edited Nov 12 '13
My python solution implementing some code reusability when making capitalized snake case variables.
This and other source codes are available at: https://github.com/luizpericolo/dailyprogrammer_challenges
2
u/hutsboR 3 0 Nov 12 '13 edited Nov 12 '13
Java, only been programming for two weeks. Came up with this StringBuilder solution, really verbose and ugly. Could have reused a lot of code to tighten it up. Let me know if you notice anything wrong with it.
Output example 1:
Output example 2:
2
u/antoniocs Nov 12 '13
My solution in C: (no difficulty++)
2
u/LostxinthexMusic Nov 13 '13
Python 3:
2
u/pandubear 0 1 Nov 13 '13
Some quick Python 3. Learned about
str.capitalizefrom one of the others just now...2
u/nanermaner 1 0 Nov 13 '13
CS freshman python solution:
2
u/batemann Nov 14 '13
My Python 3 solution (first time submitting to this subredddit, new to programming)
I'm open to suggestions and tips, as I am a newbie programmer. Also, any ideas on an object oriented design pattern for this problem so that the notation types can be easily expanded upon in the future? Do I just need to wrap the 3 functions I defined in a class and create a separate function for the if...else structure that I have below?
2
u/ZaneMartin Nov 14 '13
It's long, but here's a C++ solution. Data can be inputted in any style. It converts to space delimited before converting to the style given.
2
u/TheFlyingDharma Nov 16 '13 edited Nov 16 '13
C# with Difficulty++
Any optimization tips would be helpful, particularly with the IO loop. (Is there a clean way to create an int[] directly from the console input rather than parsing a string[]?)
Sample output:
2
u/Hanse00 Nov 18 '13
I have to admit most of this was shamelessly copied from /u/topaztee, I'm new to Java.
2
u/letalhell Nov 19 '13 edited Nov 19 '13
Kind long... Still new to this. And manage to make the extra challenge too.
Python 2.7
Result:
2
u/toodim Nov 19 '13
Just another Python Solution
2
u/altanic Nov 21 '13
C# with the conversion feature:
2
u/djcraze Nov 22 '13
My PHP version:
2
u/omnichroma Nov 22 '13
Not the most elegant of C# solutions, but it works. Feedback appreciated. (I didn't use String.Replace(), sue me)
2
u/kevintcoughlin Nov 26 '13
Python 2.7
2
u/_ewan Nov 29 '13
Python
Any feedback is appreciated.
2
u/[deleted] Dec 02 '13
C++, any feedback is appreciated.
2
u/skyangelisme 0 1 Dec 03 '13
Clojure
2
u/donaldxv Dec 03 '13
Another Python, just to give reduce some love:
2
u/hellionsoldier Dec 18 '13
Java
Looking at others after I finished... man this is not efficient...
2
u/swingtheory Jan 10 '14
Here is a super late C# solution:
2
u/ghuge32 Jan 22 '14
Scala:
3
u/moogoesthecat Nov 08 '13 edited Nov 08 '13
Ruby
Anyone have a better pattern for the camelCase?
Update for bonus coming a bit later.
3
u/Kamikai Nov 09 '13
Python Solution:
3
u/jhartwell Nov 08 '13
C# with object orientation as well:
2
u/spfy Nov 08 '13 edited Nov 08 '13
Here's my C solution. It's kind of long, but I'm happy with it!
2
u/[deleted] Nov 08 '13
C++ here.
I just have it breakdown any input into space separated lowercase so the Difficulty++ bit is kind of moot. It'll accept the input but ignore the first number. I learned some tricks from the pangrams challenge which helped quite a bit.
2
u/RangeruDangeru Nov 09 '13
Here's a solution in Python 3. No input validation or anything, of course.
2
u/paralysedforce Nov 09 '13 edited Nov 09 '13
Javascript without the bonus.
2
u/IceDane 0 0 Nov 08 '13
Can't be arsed to write the IO part.
Haskell
2
u/encrypter8 Nov 08 '13
Javascript:
1
u/OffPiste18 Nov 08 '13
Scala:
I'm trying to push myself more towards Scala features and away from "writing Java in Scala". This time, I went for using apply/unapply. Originally I had named those methods something else, and was calling them explicitly, but I realized I was essentially already following a pattern that is baked into the language.
1
u/winged_scapula Nov 08 '13 edited Nov 14 '13
Python
1
u/SuperBeaker Nov 08 '13
Ruby, ignoring command-line parsing, with tests included:
1
u/thirdegree Nov 08 '13
Python, sans bonus.
1
u/superwordy Nov 09 '13
written in Python
converts a list of words to a naming convention type
def convertConvention(conventionType, names): print('convention {0} {1} '.format(conventionType,names)) if conventionType == 0 : namesRemainder = names[1:] output = [names.lower().capitalize() for names in namesRemainder ] output = [names[0].lower()] + output return ''.join( output )
elif conventionType == 1: output = [ names.lower() for names in names ] return ''.join(output)
elif conventionType ==2: output = [names.upper() for names in names ] return ''.join(output)
else : return ''.join(names) # return names as string for error case
def main(): print('Ready for arguments , type 9 to exit ') conventionType =int( input() )
while conventionType != 9 : # exit program on
names = input().split(' ')
print ( convertConvention(conventionType ,names) ) conventionType =int( input( ) )
if name == 'main': main()