r/dailyprogrammer • u/jnazario 2 0 • May 14 '18
[2018-05-14] Challenge #361 [Easy] Tally Program
Description
5 Friends (let's call them a, b, c, d and e) are playing a game and need to keep track of the scores. Each time someone scores a point, the letter of his name is typed in lowercase. If someone loses a point, the letter of his name is typed in uppercase. Give the resulting score from highest to lowest.
Input Description
A series of characters indicating who scored a point. Examples:
abcde
dbbaCEDbdAacCEAadcB
Output Description
The score of every player, sorted from highest to lowest. Examples:
a:1, b:1, c:1, d:1, e:1
b:2, d:2, a:1, c:0, e:-2
Challenge Input
EbAAdbBEaBaaBBdAccbeebaec
Credit
This challenge was suggested by user /u/TheMsDosNerd, many thanks! If you have any challenge ideas, please share them in /r/dailyprogrammer_ideas and there's a good chance we'll use them.
37
u/brib_ May 14 '18
Python
I am teaching myself how to code and this my first ever post. I have no background in CS.
32
u/Gylergin May 14 '18 edited May 17 '18
TI-Basic: Written on my TI-84+. I take advantage of the language's use of logical results as numbers. Extraneous characters are ignored. The justification is wonky, but it's too much of a hassle to fix. Lowercase letters were unlocked via a hex-assembly program (found here) Edit: See below for an updated program. The justification is better but negative numbers are still a bother.
Input:
EbAAdbBEaBaaBBdAccbeebaec
Output:
25
u/thorwing May 14 '18
Java
read input as codepoints, map to characters. Collect to a map by grouping by lowercase character and reducing appropiatly. Finally, sorting by the value of the map and printing in order.
14
u/thestoicattack May 14 '18
bash
10
u/zatoichi49 May 14 '18
Method:
For each player in the string, take the count of lower case instances and subtract the count of upper case instances. Sort the (player, score) tuples in descending order of score, and return the results.
Python 3:
Output:
7
u/skeeto -9 8 May 14 '18
C supporting an arbitrary set of players in a–z.
13
u/Godspiral 3 3 May 14 '18 edited May 14 '18
in J, sorted alphabetically because player "e" may be missing.
first part is to construct this 3d table
edit fix: missing in between letters,
with intermediate step,
6
u/0rac1e May 15 '18 edited May 15 '18
Perl 6
Simple implementation that also scans input for Letters (according to the Unicode
Letterproperty) so invalid chars in the input will be ignored.OUTPUT:
Try it online!
5
u/datzel May 14 '18
Python 3.6. First actual post to this subreddit. Been a fan for awhile!
Output:
5
u/Nyxisto May 14 '18
Clojure
output:
["c" 3] ["d" 2] ["a" 1] ["e" 1] ["b" 0]5
u/KeinBaum May 14 '18
Scala
Challenge Output:
4
u/stanleyford May 14 '18 edited May 14 '18
F#:
4
u/octolanceae May 14 '18
C++17
Challenge output:
./tally EbAAdbBEa999BaaBBdAccbeebaec
c:3 d:2 a:1 e:1 b:0
4
u/elderron_spice May 15 '18 edited May 18 '18
C# (This is why I love LINQ)
EDIT: Implemented /u/pleky's suggestion
3
u/zck May 14 '18
Emacs Lisp:
Input:
Challenge input:
3
u/[deleted] May 14 '18 edited May 14 '18
A very non-lispy solution in Common Lisp:
Output:
Edit: Forgot to add sorting.
3
u/mesmerismo May 15 '18
JavaScript:
3
u/Chargnn May 15 '18
Java
2
u/popillol May 14 '18
Go / Golang Playground. Can only handle 5 players named abcde
2
u/lcarusLives May 14 '18
Python 3
Basic dictionary solution:
2
u/svgwrk May 14 '18 edited May 14 '18
Rust. I found the implementation for Format::new to be kind of interesting in that the method for doing that (read: sorting in descending order) in Rust is different from what I'm used to in other languages.
2
u/zqvt May 14 '18
F#
2
u/cr3z May 14 '18
written in c++, output is not the best, has a comma in the end but whatever.
2
u/notcaffeinefree May 14 '18
C#
2
u/hutsboR 3 0 May 14 '18
Elixir: I haven't been around here for over a year. I miss it.
Usage:
2
u/ogniloud May 14 '18
Perl 6
A not so idiomatic Perl 6 solution.
Output:
2
u/zelloxy May 15 '18
C#
2
u/ruincreep May 15 '18 edited May 16 '18
Perl 6. I'm sure it could be shorter and more readable at the same time, I'm probably missing some builtins that'd do all the work if I knew about them. :)
Output:
EDIT: I missed the "sorted from highest to lowest" part, fixed.
2
u/thestoicattack May 15 '18
Haskell
2
u/hi_im_nate May 15 '18
Rust
2
u/engageant May 16 '18
Posh
Output:
2
u/SuperRonJon May 16 '18
Java
My solution, not optimal and not the shortest but it gets the job done
2
u/TotalPerspective May 17 '18 edited May 17 '18
Perl Golfed it a bit
2
u/der_pudel May 21 '18
PowerShell
Output:
2
u/jpusztay May 23 '18
Java Trying to learn Java. This is the first program I ever wrote in Java.
2
u/takieyda Jun 10 '18
PowerShell
I've ben doing some work in PowerShell for my job, and so here's what I've come up with. I'm pretty new to POSH so any feedback would be appreciated.
Output
Edit: Added .ToLower() to value increments to ensure that hash table keys would remain lower case.
2
u/[deleted] Aug 09 '18
Written in Python! This is my first time ever posting code anywhere. What do you think?
2
u/GuyWood13 Aug 21 '18
PowerShell:
1
u/LegendK95 May 14 '18 edited May 14 '18
Naive Haskell - sorts on score and if scores are equal sorts on letter
1
u/iDanScott May 14 '18 edited May 14 '18
C# using dictionaries and LINQ
Challenge Output
1
u/mothamigo May 14 '18 edited May 14 '18
C
1
u/thestoicattack May 14 '18 edited May 14 '18
C++17 -- somewhat frustrating that there's no template deduction guide for
std::ostream_iterator.1
u/exfono May 14 '18
Bash
1
u/gabyjunior 1 2 May 14 '18
Ruby can manage up to 26 players [a-z], sorts results by score desc then player letter asc.
Will print nothing if input contains a symbol that is not a lower or uppercase letter.
Challenge output
1
u/chunes 1 2 May 14 '18
Factor
1
u/[deleted] May 14 '18
Python 3
Output
I'd like to know if I can somehow shorten up the initialization of the dictionary to shed a few lines.
1
u/DEN0MINAT0R May 14 '18
Python 3
Output
1
u/4gotn1 May 14 '18
AutoIt
OUTPUT
1
u/Specter_Terrasbane May 14 '18
Python 2
One-liner, just for shiggles:
Testing:
1
u/Soccer21x May 14 '18
Ruby
1
u/shepherdjay May 14 '18
Python 3.6
Built in Counter object is pretty useful for this one:
1
u/Gprime5 May 14 '18 edited May 14 '18
Python 3.6
1
u/allenguo May 14 '18
Python 2
1
u/bobthemunk May 14 '18
Python 2.7
First submission!
Would love to hear some feedback on approach, as Java is my main language and looking to improve my Python.
1
u/Average_CS_Student May 14 '18
I'm bored.
1
u/cagtbd May 14 '18
VBA
Input
Output
1
u/AnnieBruce May 15 '18
In SML/NJ.
It might be a slight abuse of the map function to update a single entry in a list, but I wanted to pull this off without using mutable references.
Challenge:
Or in a more immediately readable form:
1
u/aqrit May 15 '18 edited May 15 '18
C
players may choose any letter
1
u/RiceCake6 May 15 '18
Python 3
Probably looks pretty similar to other solutions.
Output:
1
u/Scara95 May 15 '18
Q
Works with any string of characters
Example:
1
u/investorthemolester May 15 '18
Python 3:
Input:
tally = 'EbAAdbBEaBaaBBdAccbeebaec'score = {'a' : tally.count('a') - tally.count('A'),'b' : tally.count('b') - tally.count('B'),'c' : tally.count('c') - tally.count('C'),'d' : tally.count('d') - tally.count('D'),'e' : tally.count('e') - tally.count('E')}print(sorted(score.items(), key=lambda x: x[1], reverse=True))Output:
[('c', 3), ('d', 2), ('a', 1), ('e', 1), ('b', 0)]1
u/yourbank 0 1 May 15 '18
Kotlin
Answer
1
u/errorseven May 15 '18
AutoHotkey
1
u/ChazR May 15 '18
Haskell
1
u/Philboyd_Studge 0 1 May 15 '18
Java, one liner:
1
u/Jammfire May 15 '18 edited May 15 '18
c++
Output:
1
u/brianbarbieri May 15 '18
Python 3.6
1
u/ytpies May 15 '18
Python
Uses a dict comprehension to get the scores for each player by subtracting the number of uppercase instances of that character from the number of lowercase instances. Gets the list of players by converting the whole string to lowercase, then converting it into a set to leave only the unique characters.
Then just sorts it into the right order and returns.
Output:
1
u/Mellolian May 15 '18
Python 3
import string line = 'dbbaCEDbdAacCEAadcB'
scoreLine = '' count = {} for player in line: if player in string.ascii_lowercase: if player in count: count[player] +=1 else: count[player] =1 else:
print(count)
1
u/R3DSMiLE May 15 '18
Javascript (ES6)
repl.it session
1
u/1A4Duluth May 15 '18
Here is my simple C# solution:
1
u/WaNgeL13 May 15 '18
JS: https://jsfiddle.net/7zjnzbru/1/
1
u/kdnbfkm May 15 '18 edited May 15 '18
;; Reddit /r/DailyProgrammer ;; [2018-05-14] Challenge #361 [Easy] Tally Program : dailyprogrammer ;; https://redd.it/8jcffg
Output:
Small changes https://pastebin.com/2BSMhFu9
1
u/[deleted] May 15 '18
Python 3
1
u/edixon653 May 16 '18
Python 3 - Simple and readable
1
u/[deleted] May 16 '18
F# uses the same
collapsefunction that I always find myself using in these challenges. Feedback welcome.Output:
1
u/NerdyPepper May 16 '18
RUST
A super simple rust implementation that works for any number of players. I'm pretty new to this language, suggestions are welcome :D
1
u/arthurelamothe May 16 '18 edited May 16 '18
C++
Output:
c:3 d:2 a:1 e:1 b:0
1
u/OddyseeOfAbe May 16 '18
VBA I started with this at first but was a lot of hard coding:
So I decided to go for a dictionary approach instead:
1
u/IAmTonySexual May 16 '18
C++, first checking to see if the input is valid, then using a switch statement to update an int array according to each character in the string, and finally doing some swap sorting on both the int array and a corresponding char array to organize from highest score to lowest.
.exe download || view code
1
u/burgerhex May 16 '18
Pretty simple Python 3.6 solution using f-strings, dictionaries, list comprehensions, and ternary statements.
1
u/ruineroflife May 17 '18
C# Compiled in Mono, should work in .net too anyways
Output Results for... dbbaCEDbdAacCEAadcB b:2 d:2 a:1 c:0 e:-2 Results for... EbAAdbBEaBaaBBdAccbeebaec c:3 d:2 a:1 e:1 b:0
1
u/KeenWolfPaw May 17 '18
C
1
u/saladdressing May 17 '18
Javascript:
Feedback appreciated!
Output:
1
u/Porterhouse21 May 17 '18
VBA
Output:
1
u/bebestversionofyou May 17 '18
Javascript
Input
Output
1
u/DerpinDementia May 17 '18
Python 3
Challenge
I was a bit unsure how to simplify this further for fun. All feedback welcome!
1
u/IlNomeUtenteDeve May 17 '18
Check it on my new gitHub profile: ONLY 150 line of java [
1
u/TacticalBastard May 18 '18
C
Any and all critique is welcome
1
u/pjury May 18 '18
Python3 Included a regex to verify the input.
1
u/Daanvdk 1 0 May 18 '18
C
1
u/primaryobjects May 18 '18
R
Gist | Demo
Output
1
u/govomevo May 19 '18 edited May 19 '18
SQL (Oracle):
output:
PLAYER SUM(SCORE)
c 3
d 2
a 1
e 1
b 0
1
u/borislavvv May 19 '18
Python 3.6
Output:
1
u/darknes1234 May 19 '18
C++
1
u/throwing_tantra May 20 '18
python3 one-liner, but it omits players with a negative score:
1
u/throwing_tantra May 20 '18
trying some C++:
1
u/[deleted] May 20 '18 edited May 20 '18
GDScript
(Note: GDScript is the proprietary language of the Godot Game Engine)
First time actually trying one of these. I have posted and deleted something three times, twice I posted by a misclick when I wasn't ready and third time I noticed the orders were desired. This solution is not clean, partically because of my own failing and partially because GDScript is missing out a lot of your usual tricks. It got the job done did however. Feedback very welcome!
1
u/nitishc May 21 '18
Rust
1
u/minimim May 21 '18 edited Jun 04 '18
Perl 6
OUTPUT:
Try it online!
I like feedback.
1
u/Vinas94 May 21 '18
Solution in R:
1
u/ReturnValueOther May 21 '18
JavaScript
Tried out using reduce() and chaining multiple functions together; used subarrays instead of objects so that Array.prototype.sort() could be used.
1
u/elbingobonito May 21 '18
Another Haskell solution
1
u/Endlessdex May 21 '18
Python 3:
I wanted to try using the Counter structure instead of basic lists/dicts.
1
u/penguindustin May 22 '18 edited May 22 '18
C++: Haven't coded in C++ in a while and just found C++11, CC welcome
Output:
EDIT: added output and formatting
1
u/Icenomad May 22 '18
Ruby
1
u/greenguff May 22 '18
perl
Feedback? (It's word-y, I know :/)
1
u/biepdidelibup May 23 '18
Julia
just started reading into julia a few days ago, as I am not too experienced as a programmer I would be happy about any feedback. I tried to keep it short.
Output:
1
u/throwing_tantra May 23 '18
Java
1
u/eternalblue227 May 23 '18 edited May 23 '18
Java Feedback would be appreciated.
Edit: Its my first submission on this sub.
1
u/equation_x May 23 '18
Python 3.4.2
1
u/eesov May 23 '18
Python
1
u/chcampb May 23 '18 edited May 23 '18
Semi cheating python version
1
u/jpusztay May 23 '18
Python
Made a Terminal Interface so the code can be reusable for the five players.
1
u/tk854 May 24 '18
Java
1
u/killerfridge May 24 '18 edited May 24 '18
Python 3.6 - Probably not the most efficient way of doing it, but it gets the job done:
Output:
1
u/samtaylor7 May 24 '18
Ruby First post on this subreddit. Gets the input from the first command line argument
Gist
1
u/hyrulia May 24 '18
Kotlin
1
u/BSISJ7 May 24 '18
Java
1
u/mftrhu May 25 '18
Tcl 8.6
I have been looking at Tcl for the last week or so - I had installed it on Termux to have something to fiddle with on my phone - and it has been... interesting. Surprisingly good for now, if a bit too verbose in places.
Input
Output
The original version I meant to post didn't use dictionaries - for all good they did, I'm still converting them to lists - for Tcl's arrays. I went to refactor it but discovered that arrays are more of a collection of variables than anything, and that they cannot be passed around.
1
u/chakkaveenu May 25 '18
Ruby
1
u/FinalPerfectZero May 25 '18 edited May 25 '18
C#
Output: c:3, d:2, e:1, a:1, b:0
Explanation:
1
u/marucOG May 26 '18
Python
1
u/BlasphemousJoshua May 26 '18
Swift 4.1
Output:
1
u/rebuked May 26 '18 edited May 26 '18
Haskell
Challenge output:
1
u/[deleted] May 26 '18
Python 3
Had to paste in Pastebin, didn't know how to format code in Reddit (although I tried).
https://pastebin.com/zWdKdLhR
1
u/DeathAdder10 May 26 '18
Java
1
u/[deleted] May 26 '18
C++. This is a simple, practical implementation. I haven't yet worked out how to sort the scores, but my code will correctly calculate them. Feedback welcome; I'm a CS student, so I'm always looking for ways to get better at coding!
1
u/EnvelopeBread May 27 '18
Ruby
It's been a long time since I've been able to use Ruby. I forgot so much it's embarrassing. Here's my kinda bad approach, PLEASE give me feedback.
Outputs the following:
1
u/PlsNoPics May 27 '18
This is my first shared Code ever pls be gentle :)
My Code
Output
Edit: fixed a wrong
annotationcomment1
u/sfalkson May 27 '18
written using Python
1
u/krkrkra May 28 '18 edited May 28 '18
Python 3: very much still learning Python. I've done the original Code Academy course and worked through *Automate The Boring Stuff With Python*. My only CS background is that I'm a few weeks into CS50 and MIT's 600.1 on edX. First submission to the subreddit.
1
u/theavengedCguy May 28 '18 edited May 28 '18
My attempt in PHP 7.2, but it should still work in PHP ~5.x.
Program
Input
Output
1
u/involuntary_genius May 28 '18
I am trying to learn Ruby, so here is my attempt at the challenge.
CODE
1
u/orduz May 29 '18
Python 3
1
u/LaurTe May 29 '18 edited May 29 '18
Wrote it in Python. Currently teaching myself to code Python because it's fun! Used some python modules to import few functions: from operator import itemgetter from collections import defaultdict
1
u/nso95 May 29 '18
Here's my C# solution, I'd really appreciate feedback.
1
u/whoneedsintellij May 30 '18 edited May 30 '18
Java
1
u/GreySkiesPinkShoes May 30 '18
1
u/[deleted] May 30 '18 edited May 30 '18
C++: Utilize
std::mapto avoid duplicates, then create astd::vector<pair<char,int>>using the range constructor...then realize the problem description specifies exactly 5 players, so thestd::mapstep was not necessary. Whoops! However, the end result is code that is a little more flexible and can handle up to 26 players (in the default locale).INPUT:
OUTPUT:
1
u/sakshaymahna19 May 30 '18
Python
I have written the code in Python. First, created a dictionary to store the player names and their scores. Then, defined a function that iterates over the input text comparing with the key values of the dictionary which accordingly adds and subtracts score, which finally prints the dictionary.
1
u/billc128 May 30 '18 edited May 30 '18
Elixir solution
1
u/NorthwestWolf May 31 '18
Python 3
1
u/Digabble May 31 '18
C++: First post (technically second, had formatting issues). Feedback would be much appreciated.
1
u/arachnidGrip Jun 01 '18
Haskell:
1
u/Pythonacere Jun 01 '18
Python 3
Pretty standard solution it seems. Using a dictionary to store the tallies. Sorting the dictionary by values, and then printing the result.
1
u/itachi_2017 Jun 01 '18 edited Jun 01 '18
C++
Input
Output
1
u/pbl24 Jun 01 '18
Python 2.7:
1
u/TheAnonymousCub Jun 02 '18 edited Jun 02 '18
C#
Console:
1
u/[deleted] Jun 03 '18
My Java solution
1
u/trushnick Jun 03 '18
The only question which I have in mind is this: Should answers be sorted in order of incoming scores? E.g. for
should d come first or it doesn't matter?
Python 3 (without sorting in incoming order though): from collections import defaultdict
1
u/SuasoriaSatura Jun 03 '18
Python 3.6
1
u/MusicalCoder Jun 04 '18
Python 3.6.5:
1
u/CapnNausea Jun 04 '18 edited Jun 06 '18
C#: I don't have a whole lot of experience, I'd appreciate any critiques!
**Edit: I updated so classes are created at runtime to support any number of cases and got rid of using a static lookup for score values.
1
u/netbioserror Jun 05 '18
Prepping a basic programming lesson for some friends, decided to nab a beginner challenge and try it myself before asking them to.
It's in D.
Run with
rdmd tally.d inputFor example:
rdmd tally.d dbbaCEDbdAacCEAadcB1
u/pedro_fartinez Jun 05 '18
Python 3.6
Input:
1
u/[deleted] Jun 05 '18
https://github.com/willkillson/Challenge-361-Easy-Java/tree/master
1
u/Prince_John Jun 05 '18 edited Jun 05 '18
Java
I haven't yet learned streams or any of the Java 8 goodness I've seen some other solutions use.
This produces the right answer, but seems quite convoluted. I got to a hashmap containing the correct teams and scores readily enough, but then after googling found it was quite difficult to order a hashmap by values.
My solution was just to make a Team class with custom comparator and then convert the hashmap into a list of Team objects which could then be sorted using Collections.sort.
Is there a super simple way I could have approached this without using streams?
1
u/[deleted] Jun 05 '18
Python
Not sure if this has been tried yet, but what the heck.
Input:
Output:
1
u/ilyasm97 Jun 05 '18
RemindMe!
1
u/nikewall Jun 05 '18 edited Jun 05 '18
C#: I'd love some feedback on this, since I'm just starting to dabble in C# and using dictionaries! Thanks guys and gals :)
Input: EbAAdbBEaBaaBBdAccbeebaec
1
u/Average-consumer Jun 05 '18
Haskell
Output
1
u/maggierose9194 Jun 06 '18
1
u/__8021m1208__ Jun 06 '18
Python 3
1
u/LelixSuper Jun 06 '18 edited Jun 06 '18
Assembly MIPS 32 bit
My first submission. It is not optimized (It would be funny, but I have not time). I used also macro and extended pseudo instructions provided by MARS (a program that simulates a MIPS environment).
Maybe the longest solution of this thread?
Code:
Challenge input
Challenge output
Bonus: how many instructions are executed
1
u/Aces4U Jun 06 '18
Java: I know you can do this in one line, but this is a bit easier to understand and simpler
Output:
1
u/purdygilmour Jun 07 '18
PYTHON 3 Feedback Appreciated
1
u/gabeace Jun 07 '18
C#
Was intentionally trying not to lean too heavily on Linq here. I'm trying to get better at the core code without just defaulting to Linq. Only used it to sort the list at the end. Would have used a tuple instead of a class, but I'm working on .NET 3.5.
1
u/correct_misnomer Jun 07 '18
+/u/compilebot python3
1
u/Vaakos_ Jun 09 '18
Python
First attempt to python. Have used some code from other users here to be honest.
1
u/ImportErr Jun 09 '18
Python
Im sure this isn't the best solution. Advice appreciated.
https://repl.it/@Hasan_Ahmad/Tally-Calc-Challenge
1
u/nahuak Jun 10 '18
Haven't coded in over 7 months and here's a basic attempt to review my Python skills:
1
u/xYuven Jun 10 '18
My try in C
1
u/dustinroepsch Jun 11 '18
Python, needlessly complex
1
u/risent Jun 11 '18 edited Jun 11 '18
C : I'm a Python programmer and leaning C recently, this is my first post.