r/dailyprogrammer • 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.

146 Upvotes

323 comments sorted by

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.

total_score = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}

raw_tally = 'EbAAdbBEaBaaBBdAccbeebaec'

for current_entry in raw_tally:
    if current_entry.lower() in total_score:    # making sure no bad input
        for key in total_score:
            if current_entry == key:
                total_score[key] += 1
            elif current_entry.lower() == key:  # should be matching upper case only
                total_score[key] -= 1

ordered_score = [(v, k) for k, v in total_score.items()]
                                            # "sorting"
ordered_score.sort()
ordered_score.reverse()             
ordered_score = [(k, v) for v, k in ordered_score]

print(ordered_score)

16

u/errorseven May 15 '18

Good job, keep at it!

7

u/pepejovi May 17 '18

Seems good, maybe you can figure out a way to skip the second for-loop, just as an exercise in optimization?

Something like this, if you don't want to figure it out on your own:

for current_entry in raw_tally:
    if current_entry.lower() in total_score:    # making sure no bad input
        if current_entry in total_score:
            total_score[current_entry] += 1
        elif current_entry.lower() in total_score:
            total_score[current_entry.lower()] -= 1

3

u/brib_ May 17 '18

I love the feedback.

I made a version 2.

total_score = {}
raw_tally = 'EbAAdbBEaBaaBBdAccbeebaec'

for current_entry in raw_tally:
    if current_entry.lower() not in total_score:    # auto-create key
        total_score[current_entry.lower()] = 0 
    if current_entry.isupper():                     
        total_score[current_entry.lower()] -= 1
    else:
        total_score[current_entry.lower()] += 1

print(*sorted(total_score.items(), key=lambda x: x[1], reverse=True))   # lambda function to sort
→ More replies (6)
→ More replies (3)

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.

ClrList L₁
5→dim(L₁
Input "TALLY=?",Str1
For(X,1,length(Str1
sub(Str1,X,1→Str2
(Str2="a")+2(Str2="b")+3(Str2="c")+4(Str2="d")+5(Str2="e"→N
If N≠0
Then
1+L₁(N→L₁(N
Else
(Str2="A")+2(Str2="B")+3(Str2="C")+4(Str2="D")+5(Str2="E"→N
If N≠0
L₁(N)-1→L₁(N
End
End
"abcde"→Str1
seq(X,X,1,5→L₂
SortD(L₁,L₂
For(X,1,5
Str1+sub(Str1,L₂(X),1)+" "→Str1
End
Disp sub(Str1,6,10),L₁
DelVar Str1DelVar Str2

Input:

EbAAdbBEaBaaBBdAccbeebaec

Output:

c d e a b
{3 2 1 1 0}

3

u/tikevin83 May 17 '18

Huge TI-Basic fan! I don't have my calculator with me to test this code but you can probably make more extreme use of your "logical results as numbers" concept along these lines:

ClrList L₁
10→dim(L₁
Input "TALLY=?",Str1
For(X,1,length(Str1
sub(Str1,X,1
(Ans="a")+2(Ans="b")+3(Ans="c")+4(Ans="d")+5(Ans="e")
+6(Ans="A")+7(Ans="B")+8(Ans="C")+9(Ans="D")+10(Ans="E")
1+L₁(Ans→L₁(Ans
End
seq(X,X,1,5→L₂
SortD(L₁,L₂
For(X,1,5
Output(1,2X,sub("abcde",L₂(X),1
Output(2,2X,L₁(X)-L₁(X+5
End
DelVar Str1DelVar Str2

3

u/Gylergin May 17 '18 edited May 19 '18

Nice. For some reason I always forget to use Ans in my programs. You do need to ClrHome before outputting though, and the N check was used to avoid a dimension error if an unused character got entered. I rewrote the program using your suggestions:

ClrList L₁
5→dim(L₁
Input "TALLY=?",Str1
For(X,1,length(Str1
6-inString("edcba ABCDE",sub(Str1,X,1
If Ans-6
Ans/abs(Ans)+L₁(abs(Ans→L₁(abs(Ans
End
seq(X,X,1,5→L₂
SortD(L₁,L₂
ClrHome
For(X,1,5
Output(1,2X,sub("abcde",L₂(X),1
Output(2,2X,L₁(X
End
Pause
ClrHome
DelVar Str1
→ More replies (3)

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.

	input.chars().mapToObj(i->(char)i)
	 	 .collect(groupingBy(k->toLowerCase(k), reducing(0, k -> isLowerCase(k) ? 1 : -1, Integer::sum)))
	 	 .entrySet().stream().sorted(comparingInt(e->-e.getValue()))
	 	 .forEach(System.out::println);

12

u/felinebear May 14 '18

Wow.

I really should look into Java more properly.

4

u/Philboyd_Studge 0 1 May 15 '18

I had it similar:

    System.out.println("EbAAdbBEaBaaBBdAccbeebaec".chars()
            .mapToObj(x -> (char) x)
            .collect(Collectors.groupingBy(x -> x.toString().toLowerCase(),
                    Collectors.summingInt(x -> Character.isUpperCase(x) ? -1 : 1)))
            .entrySet().stream()
            .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
            .map(Map.Entry::toString)
            .collect(Collectors.joining(", ")));
→ More replies (1)
→ More replies (8)

14

u/thestoicattack May 14 '18

bash

#!/bin/bash
fold -w1 | awk '
{
  name = tolower($0);
  scores[name] += (name == $0 ? 1 : -1);
}
END {
  for (name in scores) {
    printf("%s:%d,\n", name, scores[name]);
  }
}' | sort -t: -k2 -nr | tr "\n" " "
printf "\n"

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:

def tally(s):
    players = {i.lower() for i in s}
    res = ((i, s.count(i) - s.count(i.upper())) for i in players)
    print(*sorted(res, key=lambda x: x[1], reverse=True)) 

tally('abcde')
tally('dbbaCEDbdAacCEAadcB')
tally('EbAAdbBEaBaaBBdAccbeebaec') 

Output:

('b', 1) ('a', 1) ('d', 1) ('e', 1) ('c', 1)
('b', 2) ('d', 2) ('a', 1) ('c', 0) ('e', -2)
('c', 3) ('d', 2) ('a', 1) ('e', 1) ('b', 0)

4

u/[deleted] May 14 '18

players = {i.lower() for i in s}

Oh that's pretty neat. TIL

→ More replies (3)
→ More replies (6)

7

u/skeeto -9 8 May 14 '18

C supporting an arbitrary set of players in a–z.

#include <ctype.h>
#include <stdio.h>

int
main(void)
{
    char line[4096];
    while (fgets(line, sizeof(line), stdin)) {
        int players[26];
        int nplayers = 0;
        int score[26] = {0};
        unsigned long seen = 0;

        /* Tally up scores */
        for (char *s = line; isalpha(*s); s++) {
            int i = tolower(*s) - 'a';
            score[i] += isupper(*s) ? -1 : 1;
            if (!((seen >> i) & 1)) {
                seen |= 1UL << i;
                players[nplayers++] = i;
            }
        }

        /* Bubble sort */
        for (int i = 0; i < nplayers - 1; i++) {
            for (int j = 0; j < nplayers - i - 1; j++) {
                if (score[players[j]] < score[players[j + 1]]) {
                    int tmp = players[j];
                    players[j] = players[j + 1];
                    players[j + 1] = tmp;
                }
            }
        }

        /* Print scores */
        for (int i = 0; i < nplayers; i++)
            printf("%s%c: %d",
                    i ? ", " : "", players[i] + 'a', score[players[i]]);
        putchar('\n');
    }
}

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

 (((~. ;"0 #/.~)@}.~ ,: (~. ;"0 #/.~)@{.~) a. (1 i.~ 96&<)@i. ] )/:~ 'dbbaCEDbdAacCEAadcB'
┌─┬─┐
│a│3│
├─┼─┤
│b│3│
├─┼─┤
│c│2│
├─┼─┤
│d│3│
├─┼─┤
│ │ │
└─┴─┘

┌─┬─┐
│A│2│
├─┼─┤
│B│1│
├─┼─┤
│C│2│
├─┼─┤
│D│1│
├─┼─┤
│E│2│
└─┴─┘

   , -/ >@{:"1  (((~. ;"0 #/.~)@}.~ ,: (~. ;"0 #/.~)@{.~) a. (1 i.~ 96&<)@i. ] )/:~ 'dbbaCEDbdAacCEAadcB'
1 2 0 2 _2

, -/ >@{:"1 (((~. ;"0 #/.~)@}.~ ,: (~. ;"0 #/.~)@{.~) a. (1 i.~ 96&<)@i. ] )/:~ 'EbAAdbBEaBaaBBdAccbeebaec'
1 0 1 2 3

edit fix: missing in between letters,

(>@{: -/"1@#~ =@:|@>@{.) (~. ; #/.~) (((96 -~ a.&i.)@}.~ , (64 - a.&i.)@{.~) a. (1 i.~ 96&<)@i. ] )/:~ 'EbAAdbBEaBaaBBdAccbeebaec'
1 0 3 2 1

with intermediate step,

(((96 -~ a.&i.)@}.~ , (64 - a.&i.)@{.~) a. (1 i.~ 96&<)@i. ] )/:~ 'EbAAdbBEaBaaBBdAccbeebaec'
1 1 1 1 2 2 2 2 3 3 3 4 4 5 5 5 _1 _1 _1 _2 _2 _2 _2 _5 _5

23

u/Hobojoe_Dimaloun May 14 '18

I... I don't even know what's going on here. This is like no language I have ever seen before. Have my upvote

2

u/Scara95 May 15 '18 edited May 15 '18

Alternative:

   lower =. {&a.@(+&32^:(<&91)"0)@(a.&i.)
   upper =. {&a.@(-&32^:(>&91)"0)@(a.&i.)
   count =. 1 : '[:+/]="0 1 u'
   ((~.@lower);(~.@lower)count-((~.@upper)count)) 'dbbaCEDbdAacCEAadcB'
┌─────┬──────────┐
│dbace│2 2 1 0 _2│
└─────┴──────────┘

Edit: Adding sort:

   ({~&.:>"0 _ \:@>@(1&{)) ((~.@lower);(~.@lower)count-((~.@upper)count)) 'EbAAdbBEaBaaBBdAccbeebaec'
┌─────┬─────────┐
│cdeab│3 2 1 1 0│
└─────┴─────────┘
→ More replies (2)

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.

sub score($s) {
    my @l = $s.comb(/<:Letter>/);
    @l».lc.unique.map(-> $k { $k => @l.grep($k) - @l.grep($k.uc) }).sort(-*.value)
}

for < abcde dbbaCEDbdAacCEAadcB EbAAdbBEaBaaBBdAccbeebaec > -> $s {
    say score($s)
}

OUTPUT:

(a => 1 b => 1 c => 1 d => 1 e => 1)
(d => 2 b => 2 a => 1 c => 0 e => -2)
(c => 3 d => 2 e => 1 a => 1 b => 0)

Try it online!

6

u/0rac1e May 15 '18 edited May 15 '18

Another similar implementation, achieved by doing a Set difference operation (∖) on a Mix (a type of Multiset that supports fractional and negative weights/multiplicities).

By default, values of 0 get cancelled out, so I needed to add the final map over the unique lowercase values.

sub score($s) {
    my $m = $s.comb(/<:Ll>/) ∖ $s.comb(/<:Lu>/)».lc.Mix;
    $s.comb(/<:L>/, :g)».lc.unique.map(-> $l { $l => $m{$l} }).sort(-*.value)
}
→ More replies (1)

5

u/datzel May 14 '18

Python 3.6. First actual post to this subreddit. Been a fan for awhile!

#!/usr/bin/python3.6
# https://www.reddit.com/r/dailyprogrammer/comments/8jcffg/

def get_scores(input):
    players = {'a':0,'b':0,'c':0,'d':0,'e':0}
    for each in input:
        if each.lower() in players:
            players[each.lower()] += 1 if each >= 'a' and each <='e' else -1
    return players

if __name__ == "__main__":
    import sys
    if len(sys.argv) >= 2:
        print(get_scores(sys.argv[1]))

Output:

./tally.py abcde 
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1}

./tally.py dbbaCEDbdAacCEAadcB
{'a': 1, 'b': 2, 'c': 0, 'd': 2, 'e': -2}

./tally.py EbAAdbBEaBaaBBdAccbeebaec
{'a': 1, 'b': 0, 'c': 3, 'd': 2, 'e': 1}

2

u/datzel May 14 '18

Forgot sorting the results! Fixed below:

#!/usr/local/bin/python3.6
# https://www.reddit.com/r/dailyprogrammer/comments/8jcffg/

from operator import itemgetter

def get_scores(input):
    players = {'a':0,'b':0,'c':0,'d':0,'e':0}
    for each in input:
        if each.lower() in players:
            players[each.lower()] += 1 if each >= 'a' and each <='e' else -1
    return dict(sorted(players.items(), key=itemgetter(1), reverse=True))

if __name__ == "__main__":
    import sys
    if len(sys.argv) >= 2:
        print(get_scores(sys.argv[1]))

Output:

./tally.py abcde 
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1}

./tally.py dbbaCEDbdAacCEAadcB
{'b': 2, 'd': 2, 'a': 1, 'c': 0, 'e': -2}

./tally.py EbAAdbBEaBaaBBdAccbeebaec
{'c': 3, 'd': 2, 'a': 1, 'e': 1, 'b': 0}

5

u/Nyxisto May 14 '18

Clojure

(defn eval-char [board c]
  (let [x (if (Character/isUpperCase c) -1 1)
        cl (clojure.string/lower-case c)]
    (assoc board cl (if (board cl) (+ x (board cl)) x))))

(defn solve [input]
  (reverse (sort-by second (reduce eval-char {} input))))

output: ["c" 3] ["d" 2] ["a" 1] ["e" 1] ["b" 0]

5

u/KeinBaum May 14 '18

Scala

import scala.io.Source

object Test extends App {
  Source.stdin.getLines().map({
    _.groupBy(_.toLower)
     .mapValues(_.map(c => if(c.isUpper) -1 else 1).sum)
     .toList
     .sortBy(_._2)(Ordering[Int].reverse)
     .map(t => s"${t._1}: ${t._2}")
     .mkString(", ")
  }).foreach(println)
}

Challenge Output:

c: 3, d: 2, e: 1, a: 1, b: 0

4

u/stanleyford May 14 '18 edited May 14 '18

F#:

open System

let solve = 
    let folder (scores : Map<char, int>) player =
    let key = Char.ToLower player
    match (scores.TryFind key, if Char.IsUpper player then -1 else 1) with
        | (None, point) -> scores.Add (key, point)
        | (Some score, point) -> scores.Add (key, score + point)
    Seq.fold folder Map.empty<char, int> >> Map.toSeq

Console.ReadLine()
|> solve
|> Seq.sortBy (snd >> (~-))
|> Seq.map (fun (k,v) -> sprintf "%c:%i" k v)
|> fun s -> String.Join(" ", s)
|> printfn "%s"
→ More replies (4)

4

u/octolanceae May 14 '18

C++17

#include <algorithm>
#include <iostream>
#include <map>
#include <string_view>
#include <utility>
#include <vector>

int main(int, char** argv) {
    std::string_view score{argv[1]};
    std::map<char, int> players{};
    std::vector<std::pair<char, int>> sort_vec{};

    for (auto c: score) {
      if (std::islower(c) or std::isupper(c))
        players[std::tolower(c)] += (std::islower(c) ? 1 : -1);
    }
    for (auto p: players)
      sort_vec.emplace_back(p);

    std::sort(begin(sort_vec), end(sort_vec),
                          [](auto a, auto b) {return (a.second > b.second);});
    for (auto [p, s]: sort_vec)
      std::cout << p << ":" << s << '\t';
    std::cout << '\n';
}

Challenge output:

./tally EbAAdbBEa999BaaBBdAccbeebaec

c:3 d:2 a:1 e:1 b:0

→ More replies (4)

4

u/elderron_spice May 15 '18 edited May 18 '18

C# (This is why I love LINQ)

using Newtonsoft.Json;
using System.Linq;

namespace Implementation
{
    public class TallyProgram
    {
        public string Tally(string input)
        {
            var talliedScores = input
                .Where(char.IsLetter)
                .GroupBy(c => c)
                .Select(g => new { character = g.First().ToString(), score = (char.IsLower(g.First())) ? 1 * g.Count() : -1 * g.Count() })
                .GroupBy(g => g.character.ToLower())
                .Select(g => new { player = g.First().character.ToLower(), score = g.Sum(gg => gg.score) })
                .OrderByDescending(g => g.score);
            return JsonConvert.SerializeObject(talliedScores);
        }
    }
}

EDIT: Implemented /u/pleky's suggestion

2

u/pleky May 17 '18
.Where(char.IsLetter)    

:)

→ More replies (2)

2

u/_chebastian Jun 01 '18

Yes!, had to look through the comments to see a complete LINQ solution. Great use of anonymous types!

Tried implementing my own before reading yours but had to read yours in the end, so easy , so nice. 1 point to you! The beauty of linq is that with somewhat readable names and some fiddeling, its more readable than the shortest 'regular' solution.

→ More replies (1)

3

u/zck May 14 '18

Emacs Lisp:

(defun game-points (users score-letters)
  (string-join (mapcar (lambda (ele)
                         (format "%c:%s"
                                 (car ele)
                                 (cdr ele)))
                       (cl-sort (mapcar (lambda (user)
                                          (cons user (- (count user score-letters)
                                                        (count (upcase user) score-letters))))
                                        users)
                                #'>
                                :key #'cdr))
               ", "))

Input:

ELISP> (game-points "abcde" "dbbaCEDbdAacCEAadcB")
"b:2, d:2, a:1, c:0, e:-2"

Challenge input:

ELISP> (game-points "abcde" "EbAAdbBEaBaaBBdAccbeebaec")
"c:3, d:2, a:1, e:1, b:0"

3

u/[deleted] May 14 '18 edited May 14 '18

A very non-lispy solution in Common Lisp:

(defpackage :daily-programmer-361-easy
  (:use :cl :alexandria :arrow-macros))
(in-package :daily-programmer-361-easy)

(defun calculate-score (input)
  (let ((results (make-hash-table)))
    (loop for c across input
          for key across (string-downcase input)
          unless (gethash key results)
            do (setf (gethash key results) 0)
          do (if (lower-case-p c)
                 (incf (gethash key results))
                 (decf (gethash key results)))
          finally (return results))))

(defun sort-hash-table (hash-table)
  (alist-hash-table (sort (hash-table-alist hash-table) #'> :key #'cdr)))

(defun print-score (results)
  (maphash (lambda (k v) (format t "~A: ~A, " k v)) results))

(defun solve (input)
  (-> input
    calculate-score
    sort-hash-table
    print-score))

Output:

CL-USER> (solve "EbAAdbBEaBaaBBdAccbeebaec")
c: 3, d: 2, a: 1, e: 1, b: 0, 

Edit: Forgot to add sorting.

3

u/mesmerismo May 15 '18

JavaScript:

function tally(str) {
  return str.split("").reduce((scores, player) => {
    const lower = player.toLowerCase();
    scores[lower] = scores[lower] || 0;

    if (player == lower) {
      scores[lower] += 1;
    } else {
      scores[lower] -= 1;
    }

    return scores;
  }, {});
}

tally('abcde')
// => Object { a: 1, b: 1, c: 1, d: 1, e: 1 }
tally('dbbaCEDbdAacCEAadcB')
// => Object { d: 2, b: 2, a: 1, c: 0, e: -2 }
tally('EbAAdbBEaBaaBBdAccbeebaec')
// => Object { e: 1, b: 0, a: 1, d: 2, c: 3 }
→ More replies (1)

3

u/Chargnn May 15 '18

Java

static Map<Character, Integer> points = new HashMap<>();

static String pointsList = "EbAAdbBEaBaaBBdAccbeebaec";

public static void main(String[] args){
    points.put('a', 0);
    points.put('b', 0);
    points.put('c', 0);
    points.put('d', 0);
    points.put('e', 0);

    for(char c : pointsList.toCharArray()){
        if(Character.isUpperCase(c))
            points.put(Character.toLowerCase(c), points.get(Character.toLowerCase(c)) - 1);
        else
            points.put(c, points.get(c) + 1);
    }

    System.out.println("a" + ":" + points.get('a'));
    System.out.println("b" + ":" + points.get('b'));
    System.out.println("c" + ":" + points.get('c'));
    System.out.println("d" + ":" + points.get('d'));
    System.out.println("e" + ":" + points.get('e'));
}

5

u/g00glen00b May 16 '18

This doesn't sort the output based on the highest score.

2

u/popillol May 14 '18

Go / Golang Playground. Can only handle 5 players named abcde

package main

import (
	"bytes"
	"fmt"
	"sort"
)

func main() {
	input := []byte(`abcde
dbbaCEDbdAacCEAadcB
EbAAdbBEaBaaBBdAccbeebaec`)

	for _, in := range bytes.Split(input, []byte("\n")) {
		tally(in)
	}
}

type Player struct {
	Name  string
	Score int
}

func (p Player) String() string {
	return fmt.Sprintf("%s:%d", p.Name, p.Score)
}

func tally(in []byte) {
	playerScores := []Player{Player{"a", 0}, Player{"b", 0}, Player{"c", 0}, Player{"d", 0}, Player{"e", 0}}
	for _, v := range in {
		if v < 'a' {
			playerScores[v-'A'].Score--
		} else {
			playerScores[v-'a'].Score++
		}
	}
	sort.Slice(playerScores, func(i, j int) bool { return playerScores[i].Score > playerScores[j].Score })
	fmt.Println(playerScores)
}
→ More replies (2)

2

u/lcarusLives May 14 '18

Python 3

Basic dictionary solution:

inp = input()
uniqueDict = dict.fromkeys(''.join(set(inp.lower())), 0)

for key, val in uniqueDict.items():
    uniqueDict[key] = inp.count(key.lower()) - inp.count(key.upper())
print (uniqueDict)
→ More replies (2)

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.

use std::fmt;

fn main() {
    if let Some(history) = std::env::args().nth(1) {
        let tally = build_tally(&history);
        println!("{}", Format::new(&tally));
    }
}

fn build_tally(history: &str) -> [i8; 5] {
    let mut tally = [0; 5];

    for u in history.bytes() {
        match u {
            b'a' | b'b' | b'c' | b'd' | b'e' => tally[(u - b'a') as usize] += 1,
            b'A' | b'B' | b'C' | b'D' | b'E' => tally[(u - b'A') as usize] -= 1,

            _ => panic!("u wot m8"),
        }
    }

    tally
}

struct Format(Vec<(usize, i8)>);

impl Format {
    fn new(s: &[i8]) -> Self {
        use std::cmp::Reverse;
        let mut items: Vec<_> = s.iter().cloned().enumerate().collect();
        items.sort_by_key(|item| Reverse(item.1));
        Format(items)
    }
}

impl fmt::Display for Format {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut iter = self.0.iter();
        let mut next = iter.next();

        while let Some(&(idx, score)) = next {
            write!(f, "{}:{}", ((idx as u8) + b'a') as char, score)?;

            match iter.next() {
                None => break,
                item => {
                    next = item;
                    write!(f, ", ")?;
                }
            }
        }

        Ok(())
    }
}
→ More replies (9)

2

u/zqvt May 14 '18

F#

let update (scores: Map<char, int>) (point: char) =
  let i = if System.Char.IsUpper point then -1 else 1
  let lowc = System.Char.ToLower point
  match Map.tryFind lowc scores with
  | Some x -> Map.add lowc  (x + i) scores
  | _ -> Map.add lowc i scores

let calcScores challInput =
  Seq.fold update Map.empty challInput
  |> (Map.toSeq >> Seq.sortBy snd >> Seq.rev)
  |> Seq.iter (printf "%A ")

2

u/stanleyford May 14 '18

Hey, our solutions are almost exactly the same. Cool.

2

u/cr3z May 14 '18
include <bits/stdc++.h>
using namespace std;
int main()
{
    char temp;
    string in;
    map<int, char, std::greater<int> > out;
    map<char, int> m;
    while(cin >> in){
    	for(char& c : in) (isupper(c) ? m[tolower(c)] -=1 : m[c] +=1);
    	for(auto& it : m) out[it.second] = it.first;
    	for(auto& it : out) cout << it.second << ":" << it.first << ", ";
    	out.clear();
    	m.clear();
    }
}

written in c++, output is not the best, has a comma in the end but whatever.

2

u/thestoicattack May 14 '18

Won't this overwrite entries in out if they happen to have the same count in m ?

→ More replies (2)

2

u/notcaffeinefree May 14 '18

C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace TallyProgram
{
   internal class Player
   {
      internal int Score;
      internal char Name;

      internal Player(char name)
      {
         Name = name;
         Score = 0;
      }
   }
   
   class Program
   {
      static void Main(string[] args)
      {
         List<Player> PlayersList = new List<Player>();
         string input = "EbAAdbBEaBaaBBdAccbeebaec";

         foreach (char character in input)
         {
            if (!PlayersList.Exists(e => e.Name == char.ToLower(character)))
            {
               PlayersList.Add(new Player(char.ToLower(character)));
            }

            switch (char.ToLower(character))
            {
               case 'a':
                  if (char.IsUpper(character)) PlayersList.Find(x => x.Name == 'a').Score--;
                  else PlayersList.Find(x => x.Name == 'a').Score++;
                  break;
               case 'b':
                  if (char.IsUpper(character)) PlayersList.Find(x => x.Name == 'b').Score--;
                  else PlayersList.Find(x => x.Name == 'b').Score++;
                  break;
               case 'c':
                  if (char.IsUpper(character)) PlayersList.Find(x => x.Name == 'c').Score--;
                  else PlayersList.Find(x => x.Name == 'c').Score++;
                  break;
               case 'd':
                  if (char.IsUpper(character)) PlayersList.Find(x => x.Name == 'd').Score--;
                  else PlayersList.Find(x => x.Name == 'd').Score++;
                  break;
               case 'e':
                  if (char.IsUpper(character)) PlayersList.Find(x => x.Name == 'e').Score--;
                  else PlayersList.Find(x => x.Name == 'e').Score++;
                  break;
            }

            PlayersList = PlayersList.OrderByDescending(x => x.Score).ToList();
         }

         foreach (Player player in PlayersList)
         {
            Console.WriteLine(string.Concat(player.Name, ":", player.Score));
         }

         Console.ReadLine();
      }
   }
}

2

u/RijSw May 15 '18

You can lose the switch-case if you use

Player p = playersList.FirstOrDefault(x => x.Name == char.ToLower(character));
if(p == null) { 
    p = new Player(char.ToLower(character));
    playersList.Add(p);
}
if(char.IsUpper(character)) { p.Score--; }
else { p.Score++; }

I started with moving char.ToLower(character) to the Find method, but I ended up rewriting more :) I combined playerList.Exist and playerList.Find so there's one lookup, if it didn't find the player it creates a new one for that character. I hope you like the feedback.

2

u/hutsboR 3 0 May 14 '18

Elixir: I haven't been around here for over a year. I miss it.

defmodule Tally do
  def tally(bin) do
    Enum.reduce(String.codepoints(bin), %{}, fn e, a ->
      step = %{true: -1, false: 1}[e =~ ~r/^\p{Lu}$/u]
      Map.update(a, String.downcase(e), step, &(&1 + step))
    end)
    |> Enum.to_list()
    |> Enum.sort(fn {_, x}, {_, y} -> x > y end)
  end
end

Usage:

iex> Tally.tally("dbbaCEDbdAacCEAadcB")
[{"d", 2}, {"b", 2}, {"a", 1}, {"c", 0}, {"e", -2}]

2

u/ogniloud May 14 '18

Perl 6

A not so idiomatic Perl 6 solution.

use v6;

sub MAIN($input) {
    say scored-points($input);
}

sub scored-points(Str $str) {
    my %players;
    for $str.comb {
        when /<[a..z]>/ { %players{"$_"}++    }
        when /<[A..Z]>/ { %players{"$_".lc}-- }
    }

     my @s;
     for %players.values.sort.reverse -> $v {
         for %players.keys.sort -> $k {
             if %players{"$k"} == $v {
                @s.push($k, $v);
                 %players{"$k"}:delete;
             }
         }
      }
    return @s;
}

Output:

perl6 scores.pl6 abcde
[a 1 b 1 c 1 d 1 e 1]

perl6 scores.pl6 dbbaCEDbdAacCEAadcB
[b 2 d 2 a 1 c 0 e -2]

perl6 scores.pl6 EbAAdbBEaBaaBBdAccbeebaec
[c 3 d 2 a 1 e 1 b 0]
→ More replies (1)

2

u/zelloxy May 15 '18

C#

	var input = "EbAAdbBEaBaaBBdAccbeebaec";
	input.ToList().GroupBy(m => Char.ToLower(m), v => Char.IsLower(v) ? 1 : -1).OrderByDescending(m => m.Sum()).ToList().ForEach(m => {
		Console.WriteLine(m.Key + ":" + m.Sum().ToString());
	});

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. :)

with slurp.comb(/<:Letter>/).cache {
  my $scores = [(-)] .classify(*.uniprop('Uppercase')).pairs.sort(*.key)>>.value>>.lc.map(*.Mix);
  say $scores{.map(*.lc).unique}:!p.sort(-*.value);
}

Output:

(c => 3 d => 2 a => 1 e => 1 b => 0)

EDIT: I missed the "sorted from highest to lowest" part, fixed.

2

u/thestoicattack May 15 '18

Haskell

module Tally (main) where

import Data.Char (isLower, toLower)
import Data.List (foldl', sort)

data Score = Score Char Int
instance Show Score where show (Score c v) = c:':':(show v)
instance Eq Score where (Score _ a) == (Score _ b) = a == b
instance Ord Score where compare (Score _ a) (Score _ b) = compare a b

update :: [Score] -> Score -> [Score]
update [] s = [s]
update (x@(Score c v):xs) s@(Score c' v') =
  if c == c' then (Score c (v + v')):xs else x:(update xs s)

tally :: String -> [Score]
tally s = foldl' update [] $ map toScore s
  where toScore c = Score (toLower c) (if isLower c then 1 else -1)

main :: IO ()
main = interact $ unlines . map (show . reverse . sort . tally) . lines

2

u/hi_im_nate May 15 '18

Rust

use std::collections::HashMap;
use std::io;
use std::io::BufRead;

fn main() {
    let stdin = io::stdin();
    let mut lock = stdin.lock();
    let mut line = String::new();
    loop {
        line.clear();
        lock.read_line(&mut line).expect("Failed to read line");

        let mut scores = HashMap::new();
        line.trim().chars().for_each(|c| {
            let idx = c.to_lowercase().collect::<Vec<char>>()[0];
            let num = scores.entry(idx).or_insert(0);
            if c.is_lowercase() {
                *num += 1;
            } else {
                *num -= 1;
            }
        });

        let mut results = scores.into_iter().collect::<Vec<(char, i32)>>();
        results.sort_by_key(|(_, v)| -*v);

        println!("{:?}", results);
    }
}

2

u/engageant May 16 '18

Posh

$inLower = "a", "b", "c", "d", "e"
$inUpper = $inLower.ToUpper()
$scoreCard = "EbAAdbBEaBaaBBdAccbeebaec" 
$scoreTally = @{}

foreach ($letter in $inLower) {    
    $scoreTally[$letter] += ([regex]::Matches($scoreCard, $letter)).Count
}
foreach ($letter in $inUpper) {    
    $scoreTally[$letter] -= ([regex]::Matches($scoreCard, $letter)).Count
}

$scoreTally.GetEnumerator() | select @{n = 'Player'; e = {$_.Name}}, @{n = 'Score'; e = {$_.Value}} | sort -Descending -Property Score

Output:

Player Score
------ -----
c          3
d          2
a          1
e          1
b          0

2

u/SuperRonJon May 16 '18

Java

My solution, not optimal and not the shortest but it gets the job done

public class Tally {
    public static void main(String[] args){
        String input = "EbAAdbBEaBaaBBdAccbeebaec";
        printTally(input);
    }

    public static void printTally(String scores){
        Map<Character, Integer> tallies = new HashMap<>();
        char[] scoreArray = scores.toCharArray();
        for(char score : scoreArray){
            if(tallies.containsKey(Character.toLowerCase(score))){
                int count = tallies.get(Character.toLowerCase(score));
                tallies.put(Character.toLowerCase(score), Character.isUpperCase(score) ? count - 1 : count + 1);
            }
            else{
                tallies.put(Character.toLowerCase(score), Character.isUpperCase(score) ? -1 : 1);
            }
        }

        printMap(tallies);
    }

    private static void printMap(Map<Character, Integer> map){
        map.entrySet()
                .stream()
                .sorted(Comparator.comparing(Map.Entry<Character, Integer>::getValue).reversed ())
                .forEach(System.out::println);
    }
}

2

u/TotalPerspective May 17 '18 edited May 17 '18

Perl Golfed it a bit

printf 'EbAAdbBEaBaaBBdAccbeebaec' | perl -nE 'for (/./g) {if (uc$_ eq $_){$p{lc$_}--}else{$p{lc$_}++}}; say join(",", map{"$_:$p{$_}"}sort {$p{$a} <=> $p{$b}} keys %p)'
b:0,e:1,a:1,d:2,c:3

2

u/der_pudel May 21 '18

PowerShell

$input = "EbAAdbBEaBaaBBdAccbeebaec"
$hash = @{}

For ($i=0; $i -le $input.Length -1; $i++) {
    $ch = $input.substring($i,1)
    If (-not $hash.ContainsKey($ch.ToLower())) {
        $hash.Add(  $ch.ToLower(), 0)
    }
    If ($ch -ceq $ch.ToLower() ) {
        $hash[$ch]++
    } Else {
        $hash[$ch]--
    }
}

Write-Host ($hash.GetEnumerator() | sort -Property value -Descending | Out-String)

Output:

Name                           Value                                                                                                                              
----                           -----                                                                                                                              
c                              3                                                                                                                                  
d                              2                                                                                                                                  
e                              1                                                                                                                                  
a                              1                                                                                                                                  
b                              0  
→ More replies (1)

2

u/jpusztay May 23 '18

Java Trying to learn Java. This is the first program I ever wrote in Java.

import java.util.HashMap;

public class TallyProgram {

	    HashMap<Character,Integer> scores = new HashMap<Character,Integer>();

    public void setUp(String char_series) {
	    String players = new String("abcde");
	    for(int i = 0, n = players.length() ; i < n ; i++) { 
	        char c = players.charAt(i);
	        scores.put(c, 0);
	    }
    }
    public void losePoint(char ch) {
	    if(ch == Character.toUpperCase(ch)) {
		    char c = Character.toLowerCase(ch);
		    scores.put(c, scores.get(c) - 1);
	    }
    }
    public void gainPoint(char ch) {
	    if(ch == Character.toLowerCase(ch)) {
		    char c = Character.toLowerCase(ch);
		    scores.put(c, scores.get(c) + 1);
	    }
    }
    public static void main(String[] args) {
	    TallyProgram test1 = new TallyProgram();
	    test1.setUp("abcde");
	    String tally = new String("EbAAdbBEaBaaBBdAccbeebaecACCCDDE");
	    for(int i = 0, n = tally.length() ; i < n; i++) {
	   	    char c = tally.charAt(i);
		    test1.losePoint(c);
		    test1.gainPoint(c);
	    }
    System.out.println(test1.scores);	
    }
}

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.

$score = [ordered]@{"a"=0; "b"=0; "c"=0; "d"=0; "e"=0}

$points = "dbbaCEDbdAacCEAadcB" #Read-Host "Points this round?"

ForEach ($player in $points.GetEnumerator()) {
    $player = $player.ToString()

    If ($player -ceq $player.ToLower()) {
    $score[$player.ToLower()]+=1
    } Else {
        $score[$player.ToLower()]-=1
    }
}

$score.GetEnumerator() | Sort-Object -Property value -Descending

Output

Name                           Value                                                                        
----                           -----                                                                        
b                              2                                                                            
d                              2                                                                            
a                              1                                                                            
c                              0                                                                            
E                              -2 

Edit: Added .ToLower() to value increments to ensure that hash table keys would remain lower case.

2

u/Lee_Dailey Jul 26 '18

howdy takieyda,

i never seem to think of the enumerators. [grin] nice code!

however, i think you got the wrong input string. that is the demo, not the one we were supposed to work on.

take care,
lee

→ More replies (2)

2

u/[deleted] Aug 09 '18

Written in Python! This is my first time ever posting code anywhere. What do you think?

def find_score(input):
    tally = {'a':0, 'b':0, 'c':0, 'd':0, 'e':0}
    for key in tally:
        tally[key] = input.count(key.lower()) - input.count(key.upper())
    print sorted(tally.values(), reverse=True)

find_score("EbAAdbBEaBaaBBdAccbeebaec")

2

u/GuyWood13 Aug 21 '18

PowerShell:

$cInput = "EbAAdbBEaBaaBBdAccbeebaec"
$cInputArray = @()
$cInputArray = $cInput.ToCharArray()

$tally = @{}
 $tally["a"] = 0;
 $tally["b"] = 0;
 $tally["c"] = 0;
 $tally["d"] = 0;
 $tally["e"] = 0;

ForEach ($letter in $cInputArray) {
    If ($letter -cmatch "[A-Z]") {
        $tally["$letter"]--
    }
    Else {
        $tally["$letter"]++

    }
}

$tally.GetEnumerator() | Sort-Object Value -Descending

1

u/LegendK95 May 14 '18 edited May 14 '18

Naive Haskell - sorts on score and if scores are equal sorts on letter

import Data.Char (toLower, toUpper)
import Data.List (nub, sortBy)

tally :: String -> [(Char, Int)]
tally xs = sortBy cmp $ map (\c -> (c, occ c - occ (toUpper c))) xs'
  where xs' = nub $ map toLower xs
        occ c = length $ filter (==c) xs
        cmp a b = (snd b `compare` snd a) `mappend` (fst a `compare` fst b)

1

u/iDanScott May 14 '18 edited May 14 '18

C# using dictionaries and LINQ

using System;
using System.Collections.Generic;
using System.Linq;

namespace TallyProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<char, int> scoreTable = new Dictionary<char, int>();
            string input = "EbAAdbBEaBaaBBdAccbeebaec";

            for (int i = 0; i < input.Length; i++)
            {
                if (scoreTable.ContainsKey(char.ToLower(input[i])))
                    scoreTable[char.ToLower(input[i])] += char.IsUpper(input[i]) ? -1 : 1;
                else
                    scoreTable.Add(char.ToLower(input[i]), char.IsUpper(input[i]) ? -1 : 1);
            }

            Dictionary<char, int> ordered = scoreTable
                                            .OrderByDescending(x => x.Value)
                                            .ToDictionary(x => x.Key, x => x.Value);

            for (int i = 0; i < ordered.Count; i++)
            {
                Console.Write($"{ordered.Keys.ElementAt(i)}:" +
                    $"{ordered[ordered.Keys.ElementAt(i)]}" +
                    $"{(i == ordered.Count - 1 ? "\n" : ", ")}");
            }
        }
    }
}

Challenge Output

c:3, d:2, e:1, a:1, b:0 

1

u/mothamigo May 14 '18 edited May 14 '18

C

#include <stdio.h> // printf()

typedef struct {
	char c;
	int score;
} player;

int main(int argc, char *argv[]) { // argv[1] is the score string
	int i, swapped;
	player temp;
	/* initialize */
	player freq[5];
	for (i = 0; i < 5; i++) {
		freq[i].c = 'a' + i;
		freq[i].score = 0;
	}
	/* count scores */
	i = -1;
	while (argv[1][++i]) {
		argv[1][i] < 'a' ? freq[argv[1][i] - 'A'].score-- : freq[argv[1][i] - 'a'].score++;
	}
	/* bubble sort */
	do {
		swapped = 0;
		for (i = 0; i < 4; i++) {
			if (freq[i].score < freq[i+1].score) {
				swapped = 1;
				temp = freq[i];
				freq[i] = freq[i+1];
				freq[i+1] = temp;
			}
		}
	} while (swapped);
	/* print */
	for (i = 0; i < 5; i++) {
		printf("%c:%d\n", freq[i].c, freq[i].score);
	}
}

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 .

#include <algorithm>
#include <cctype>
#include <iterator>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

namespace {

struct Score {
  char name;
  int value = 0;

  explicit Score(char c) : name(c) {}

  bool operator>(Score s) const {
    return value > s.value;
  }
};

std::ostream& operator<<(std::ostream& out, Score s) {
  return out << s.name << ':' << s.value;
}

auto tally(std::string_view points) {
  std::vector<Score> scores;
  for (auto c : points) {
    auto n = std::tolower(static_cast<unsigned char>(c));
    auto it = std::find_if(
        scores.begin(), scores.end(), [n](auto s) { return s.name == n; });
    auto& s = it == scores.end() ? scores.emplace_back(n) : *it;
    s.value += (std::islower(static_cast<unsigned char>(c)) ? 1 : -1);
  }
  return scores;
}

}  // namespace

int main() {
  std::string line;
  while (std::getline(std::cin, line)) {
    auto scores = tally(line);
    std::sort(scores.begin(), scores.end(), std::greater<>{});
    std::copy(
        scores.begin(),
        scores.end(),
        std::ostream_iterator<decltype(scores)::value_type>(std::cout, ", "));
    std::cout << '\n';
  }
}

1

u/exfono May 14 '18

Bash

#!/bin/bash
for c in a b c d e
do
    printf $c: >> out.tmp
    echo 2*`echo $1 | sed "s/[^$c]//g" | wc -m`-`echo $1 | tr [:upper:] [:lower:] | sed "s/[^$c]//g" | wc -m` -1 | bc >>out.tmp
done
sort -g -r -k2 -t':' out.tmp
rm out.tmp

3

u/thestoicattack May 14 '18

You don't really need out.tmp, since you can pipe everything out of your for loop, like

for c in a b c d e; do
  printf $c:
  echo [... complicated part ...]
done | sort -g -r -k2 -t:
→ More replies (1)

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.

def add_player_score(results, player, score)
    if results[player].nil?
        results[player] = score
    else
        results[player] += score
    end
end

if ARGV.size != 1
    exit false
end
results = Hash.new
ARGV[0].chars.each do |player|
    if player =~ /[a-z]/
        add_player_score(results, player, 1)
    elsif player =~ /[A-Z]/
        add_player_score(results, player.downcase, -1)
    else
        exit false
    end
end
results.sort_by do |player, score| [-score, player] end.each do |player, score|
    puts "#{player}:#{score}"
end

Challenge output

c:3
d:2
a:1
e:1
b:0

1

u/chunes 1 2 May 14 '18

Factor

USING: formatting kernel math qw sequences sorting strings ;
IN: dailyprogrammer.tally-program

CONSTANT: friends "abcde"

: score ( seq n -- m ) 2dup 32 - [ [ = ] curry count ] 2bi@ - ;

: scores ( seq -- seq ) friends [ score ] with { } map-as ;

: tally ( seq -- assoc )
    scores friends [ 1string swap { } 2sequence ] 2map
    sort-values reverse ;
    
qw{ abcde dbbaCEDbdAacCEAadcB EbAAdbBEaBaaBBdAccbeebaec }
[ tally "%[%s: %d %]\n" printf ] each

1

u/[deleted] May 14 '18

Python 3

input = 'dbbaCEDbdAacCEAadcB'

scores = {}
for letter in input:
    if letter.lower() not in scores:
        scores[letter.lower()] = 0
    scores[letter.lower()] += 1 if letter.islower() else -1
print(', '.join(['{0}:{1}'.format(i, scores[i]) for i in scores]))

Output

d:2, b:2, a:1, c:0, e:-2

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

str = input('> ')

scores = {}
for l in str:
    if l.lower() in scores.keys():
        scores[l.lower()] = scores[l.lower()] + 1 if l == l.lower() else scores[l.lower()] - 1
    else:
        scores[l.lower()] = 1 if l == l.lower() else -1

sorted_scores = [(k, scores[k]) for k in sorted(scores, key=scores.get, reverse=True)]
for s in sorted_scores:
    print(s)

Output

('c', 3)
('d', 2)
('e', 1)
('a', 1)
('b', 0)

1

u/4gotn1 May 14 '18

AutoIt

#include <Array.au3>

ConsoleWrite(_ArrayToString(TallyPoints("EbAAdbBEaBaaBBdAccbeebaec")))

Func TallyPoints($input)
	Local $pointsArray = StringSplit($input, "")
	Local $playerArray = [ ['a', 0], ['b', 0], ['c', 0], ['d', 0], ['e', 0] ]

	For $i = 0 to UBound($playerArray) - 1
		For $j = 1 to UBound($pointsArray) - 1
			If StringIsLower($pointsArray[$j]) AND $playerArray[$i][0] = $pointsArray[$j] Then
				$playerArray[$i][1] += 1
			ElseIf StringIsUpper($pointsArray[$j]) AND $playerArray[$i][0] = $pointsArray[$j] Then
				$playerArray[$i][1] -= 1
			EndIf
		Next
	Next
	
	;~ Sort the player array in descending order of points
	_ArraySort($playerArray, 1, 0, 0, 1)
	
	Return $playerArray

EndFunc ;==> TallyPoints

OUTPUT

c|3
d|2
e|1
a|1
b|0

1

u/Specter_Terrasbane May 14 '18

Python 2

One-liner, just for shiggles:

tally = lambda s: ', '.join('{}:{}'.format(*i) for i in sorted(reduce(lambda a, u: a.__setitem__(u.lower(), a.get(u.lower(), 0) + (1, -1)[u.isupper()]) or a, s, {}).items(), key=lambda (p, n): (-n, p)))

Testing:

>>> [tally(s) for s in ('abcde', 'dbbaCEDbdAacCEAadcB', 'EbAAdbBEaBaaBBdAccbeebaec')]
['a:1, b:1, c:1, d:1, e:1', 'b:2, d:2, a:1, c:0, e:-2', 'c:3, d:2, a:1, e:1, b:0']

1

u/Soccer21x May 14 '18

Ruby

scores = {}
input = 'dbbaCEDbdAacCEAadcB'
input_array = input.split('')
players = input_array.collect{|letter| letter.downcase}.uniq

players.each do |player|
  player_negative = player.upcase
  score = input_array.count(player) - input_array.count(player_negative)
  scores[player] = score
end

puts scores
→ More replies (1)

1

u/shepherdjay May 14 '18

Python 3.6

Built in Counter object is pretty useful for this one:

from collections import Counter


def main(score_str):
    wins = Counter([win for win in score_str if win.islower()])
    wins.subtract(Counter([loss.lower() for loss in score_str if loss.isupper()]))
    return wins


if __name__ == '__main__':
    score_str = 'dbbaCEDbdAacCEAadcB'
    [print(f'{player}: {wins}') for player, wins in main(score_str).items()]
→ More replies (2)

1

u/Gprime5 May 14 '18 edited May 14 '18

Python 3.6

def challenge(p):
    return sorted({k:(p.count(k)-p.count(k.upper())) for k in set(p.lower())}.items(), key=lambda x:-x[1])

print(challenge("abcde"))
# [('b', 1), ('a', 1), ('c', 1), ('d', 1), ('e', 1)]
print(challenge("dbbaCEDbdAacCEAadcB"))
# [('b', 2), ('d', 2), ('a', 1), ('c', 0), ('e', -2)]

1

u/allenguo May 14 '18

Python 2

from collections import Counter

def tally(s):
    c = Counter(filter(str.islower, s))
    c.subtract(Counter(filter(str.isupper, s).lower()))
    return ', '.join(k + ':' + str(v) for k, v in c.most_common())

print tally('abcde')
print tally('dbbaCEDbdAacCEAadcB')

1

u/bobthemunk May 14 '18

Python 2.7

First submission!

import operator
players = ['a', 'b', 'c', 'd', 'e']
game = {}
scores = raw_input("Enter scores: ")

for player in players:
    game[player] = 0

for score in scores:
    if score in players:
        game[score] += 1
    elif score.lower() in players:
        game[score.lower()] -= 1
    else:
        continue
print(sorted(game.items(), key=operator.itemgetter(1), reverse=True))

Would love to hear some feedback on approach, as Java is my main language and looking to improve my Python.

→ More replies (2)

1

u/Average_CS_Student May 14 '18
import re


def tally(points: str) -> None:
    """ Output the scores of all players from highest to lowest.
    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.
    :param points: The series of characters indicating who scored a point.

    Example :
    >>> tally("EbAAdbBEaBaaBBdAccbeebaec")
    c:3, d:2, e:1, a:1, b:0
    """
    scores = dict()
    for char in points:
        _update_scores(scores, char)
    output = _format_output(scores)
    print(output)


def _update_scores(scores: dict, char: str) -> None:
    try:
        scores[char.lower()] += _char_to_points(char)
    except KeyError:
        scores[char.lower()] = _char_to_points(char)


def _char_to_points(char: str, WIN_VALUE: int = 1,
                    LOSE_VALUE: int = -1) -> int:
    if char.islower():
        return WIN_VALUE
    return LOSE_VALUE


def _format_output(scores: str) -> str:
    output = sorted(scores.items(),
                    key=lambda p: p[1],  # Sorting the dict by its values
                    reverse=True         # Sorting from highest to lowest
                    ).__str__()
    output = re.sub(r'[\[\]]', '', output)
    output = re.sub(r'\(\'(\w)\'\, (\d)\)', r'\1:\2', output)
    return output


def main():
    tally("EbAAdbBEaBaaBBdAccbeebaec")


if __name__ == '__main__':
    main()

I'm bored.

1

u/cagtbd May 14 '18

VBA

Sub challenge()
Dim string_in As String
Dim str_1 As String

    Columns("A:B").ClearContents
    With Range("A1")
    .Offset(0, 0) = "a"
    .Offset(1, 0) = "b"
    .Offset(2, 0) = "c"
    .Offset(3, 0) = "d"
    .Offset(4, 0) = "e"
    End With
    

    string_in = Range("E1").Value
    While Not string_in = ""
    str_1 = Left(string_in, 1)
    lower_ = LCase(str_1)
    check_me = LCase(str_1)
    
    If check_me = str_1 Then
        add_me = 1
    Else
        add_me = -1
    End If
    
    Cells(Asc(check_me) - 96, 2) = Cells(Asc(check_me) - 96, 2).Value + add_me
    string_in = Right(string_in, Len(string_in) - 1)
    Wend



    
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1:B5"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1:A5"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:B5")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
End Sub

Input

EbAAdbBEaBaaBBdAccbeebaec

Output

c	3
d	2
a	1
e	1
b	0

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.

fun tally_score(scores) =
    let fun adjust_score (f, score, results) = 
        map (fn (p, s) => if p = score
			  then (p, f (s))
			  else (p, s))
	    results;
    fun tally (scores, results) =
	case scores of
	    [] => results
	  | score::scores' => let val s = Char.toLower(score)
				  val e = List.exists (fn x => #1 x = s) results;
			      in  if Char.isLower(score)
				  then if e then tally(scores', adjust_score((fn x => x + 1), s, results))
				       else tally(scores', (s, 1)::results)
				  else if e then tally(scores', adjust_score((fn x => x - 1), s, results))
				  else tally(scores', (s, ~1)::results)
			      end;
in
    tally (scores, [])
end

Challenge:

val it = [(#"c",3),(#"d",2),(#"a",1),(#"b",0),(#"e",1)] : (char * int) list

Or in a more immediately readable form:

c:3, d:2, a:1, b:0, e:1
→ More replies (1)

1

u/aqrit May 15 '18 edited May 15 '18

C
players may choose any letter

#include <stdio.h>

void sort(int *a, int n) {
    for(int i = 1; i < n; ++i) {
        int tmp = a[i];
        int j = i;
        while(j > 0 && tmp > a[j - 1]) {
            a[j] = a[j - 1];
            --j;
        }
        a[j] = tmp;
    }
}

void score (char* input){
    int count[256];
    int players = 0;
    for (int i = 0; i < 256; i++) count[i] = 0;
    for (char* p = input; *p != '\0'; p++) count[*p]++;
    for (char c = 'a'; c <= 'z'; c++) {
        count[players] = ((count[c] - count[c - 0x20]) << 8) | (~c & 0xFF);
        if (count[c] || count[c - 0x20]) players++;
    }
    sort(count, players);
    for (int i = 0; i < players; i++) printf("%c:%d ",  ~(count[i] & 0xFF) & 0xFF, count[i] >> 8);
    printf("\n");
}

int main () {
    score("abcde");
    score("dbbaCEDbdAacCEAadcB");
    score("EbAAdbBEaBaaBBdAccbeebaec");
    score("buzzZ");
    return 0;
}

1

u/RiceCake6 May 15 '18

Python 3

Probably looks pretty similar to other solutions.

def tally(seq):
   tally = {c : seq.count(c) - seq.count(c.upper()) for c in set(seq.lower())}                    
   sorted_keys = sorted(tally.keys(), key=lambda c:-tally[c])
   print(', '.join(["{}:{}".format(c, tally[c]) for c in sorted_keys]))
    
tally('abcde')
tally('dbbaCEDbdAacCEAadcB')
tally('EbAAdbBEaBaaBBdAccbeebaec') 

Output:

c:1, d:1, e:1, b:1, a:1
d:2, b:2, a:1, c:0, e:-2
c:3, d:2, e:1, a:1, b:0

1

u/Scara95 May 15 '18

Q

Works with any string of characters

{a: distinct lower x; c:{sum each y =\: x}[x]; desc a!c[a]-c[upper a]}

Example:

q){a: distinct lower x; c:{sum each y =\: x}[x]; desc a!c[a]-c[upper a]} "dbbaCEDbdAacCEAadcB"
d| 2
b| 2
a| 1
c| 0
e| -2
q){a: distinct lower x; c:{sum each y =\: x}[x]; desc a!c[a]-c[upper a]} "EbAAdbBEaBaaBBdAccbeebaec"
c| 3
d| 2
e| 1
a| 1
b| 0

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)]

2

u/RiceCake6 May 15 '18

While this solution works, hardcoding the values probably isn't what you want to do. Using some kind of list/dictionary comprehension along with a way of subtracting lowercase counts from uppercase counts would be a lot cleaner!

1

u/yourbank 0 1 May 15 '18

Kotlin

val input = "EbAAdbBEaBaaBBdAccbeebaec"

val results = input.groupBy { it.toLowerCase() }
        .mapValues {
            it.value.fold(0, { acc, next ->
                if (next.isLowerCase()) acc + 1 else acc - 1
            })
        }.entries.sortedByDescending { it.value }

Answer

[c=3, d=2, e=1, a=1, b=0]

1

u/errorseven May 15 '18

AutoHotkey

tally(str) {
    
    score := {a: 0, b:0, c:0, d:0, e: 0}
    
    for e, v in StrSplit(str)
        asc(v) < 97 ? score[(v)]-- : score[(v)]++
    
    for e, v in score.clone() {
        results .= Format("{1}: {2} ", x:=max(score, 1), score.delete(x))
    }
    
    return results
}

Max(obj, key:=0) {

    for e, v in obj {
        if (v > max || max == "")
            max := v, k := e
    }
            
    return key ? k : max
}

1

u/ChazR May 15 '18

Haskell

import Data.Char
import Data.List (sortBy)
import System.Environment (getArgs)

has_key ts key =  any (==key) [k | (k,_) <- ts]

update ts key amount
  | has_key ts key = let update_if_key t = if (fst t) == key
                                         then (fst t, amount + (snd t))
                                         else t
                                              in [update_if_key t | t <- ts]
  | otherwise = (key, amount):ts

tally "" = []
tally (c:cs)
  | isAsciiUpper c = update t (toLower c) (-1)
  | isAsciiLower c = update t c 1
                     where t = tally cs 

print_tally [] = return ()
print_tally((k,v):ts) = do
  putStrLn $ (k:"") ++ ": " ++ show v      
  print_tally ts
  
main = do
  string:_ <- getArgs
  print_tally $ sortBy (\(_,a) (_,b)-> compare b a) $ tally string

1

u/Philboyd_Studge 0 1 May 15 '18

Java, one liner:

    System.out.println("EbAAdbBEaBaaBBdAccbeebaec".chars()
            .mapToObj(x -> (char) x)
            .collect(Collectors.groupingBy(x -> x.toString().toLowerCase(),
                    Collectors.summingInt(x -> Character.isUpperCase(x) ? -1 : 1)))
            .entrySet().stream()
            .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
            .map(Map.Entry::toString)
            .collect(Collectors.joining(", ")));

1

u/Jammfire May 15 '18 edited May 15 '18

c++

#include "stdafx.h"
#include "map"
#include "iostream"
#include <string>>
#include <vector>
#include <algorithm>

using namespace std;


int main()
{
map<char, int> players;

players['a'] = 0;
players['b'] = 0;
players['c'] = 0;
players['d'] = 0;
players['e'] = 0;

string input = "EbAAdbBEaBaaBBdAccbeebaec";
char currentLetter;

for (int i = 0; i<input.length(); i++)
{
	currentLetter = input[i];

	if (isupper(currentLetter))
		players[tolower(currentLetter)] -= 1;
	else
		players[currentLetter] += 1;
}

typedef pair<char, int> pair;
vector<pair> vec;

copy(players.begin(), players.end(), back_inserter<vector<pair>>(vec));

sort(vec.begin(), vec.end(), [](const pair& l, const pair& r) {
	if (l.second != r.second)
		return l.second > r.second;

	return l.first > r.first;
});


for (auto const &pair : vec)
	cout << pair.first << ":" << pair.second << endl;

return 0;
 }

Output:

c:3
d:2
e:1
a:1
b:0

1

u/brianbarbieri May 15 '18

Python 3.6

def Tally(string):
  output = {}
  for letter in list(string):
    if letter.lower() not in output.keys():
      output[letter.lower()] = 0
    if letter.islower():
      output[letter.lower()] += 1
    if letter.isupper():
      output[letter.lower()] -= 1
  
  ordered = list(reversed(sorted(output, key=output.get))) 
  return ''.join('{}:{} '.format(letter , output[letter]) for letter in ordered)
    
inp1 = 'abcde'
inp2 = 'dbbaCEDbdAacCEAadcB'
inp3 = 'EbAAdbBEaBaaBBdAccbeebaec'

print(Tally(inp1))  
print(Tally(inp2))
print(Tally(inp3))

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.

def tallyScores(scoreLine):
    scores = {tally: (scoreLine.count(tally) - scoreLine.count(tally.upper())) for tally in set(scoreLine.lower())}
    return sorted(scores.items(), key=lambda x: x[1], reverse=True)

print(tallyScores("EbAAdbBEaBaaBBdAccbeebaec"))

Output:

[('c', 3), ('d', 2), ('e', 1), ('a', 1), ('b', 0)]

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:

    if player.lower() in count:
        count[player.lower()] -=1
    else:
        count[player.lower()] = -1

print(count)

1

u/R3DSMiLE May 15 '18

Javascript (ES6)

function tallyScore(input = '') {
  const hash = {};
  const getHash = (l) => hash[l.toLowerCase()];
  const setHash = (l, v) => {hash[l.toLowerCase()] += v;}

  input.split('').forEach(letter => {

    if (!getHash(letter)) hash[letter.toLowerCase()] = 0;
    if (letter.toUpperCase() === letter) setHash(letter, -1)
    else setHash(letter, 1);
  });

  return Object.entries(hash).map(val => `${val[0]}: ${val[1]}`).join(', ');
}

console.log(tallyScore('abcde'))
console.log(tallyScore('dbbaCEDbdAacCEAadcB'))

repl.it session

1

u/1A4Duluth May 15 '18

Here is my simple C# solution:

    class Program
    {
        static void Main(string[] args)
        {
            ComputeResults("abcde");
            ComputeResults("dbbaCEDbdAacCEAadcB");
            ComputeResults("EbAAdbBEaBaaBBdAccbeebaec");
            Console.ReadKey();
        }
        public static void ComputeResults(string stats)
        {
            string players = "abcde";
            Dictionary<char, int> scores = new Dictionary<char, int>();

            foreach(char player in players)
            {
                scores.Add(player, 0);
            }

            foreach(char stat in stats)
            {
                if (char.IsUpper(stat))
                {
                    scores[char.ToLower(stat)]--;
                }
                else
                {
                    scores[stat]++;
                }
            }

            foreach (KeyValuePair<char, int> score in scores.OrderByDescending(a => a.Value))
            {
                Console.Write($"{score.Key}: {score.Value}    ");
            }

            Console.WriteLine();
        }
    }

1

u/WaNgeL13 May 15 '18

JS: https://jsfiddle.net/7zjnzbru/1/

const ourScore = `EbAAdbBEaBaaBBdAccbeebaec`

const sortObject = (obj) => {
  const arr = []
  let prop
  for (prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      arr.push({
        key  : prop,
        value: obj[prop],
      })
    }
  }
  arr.sort((a, b) => b.value - a.value)
  return arr
}

const getPlayers = (score) => {
  const scoreArrayLowered = score.toLowerCase().split(``)
  return [...new Set(scoreArrayLowered)].reduce((acc, item) => {
    acc[item] = 0
    return acc
  }, {})
}

const scoreCalc = (score) => {
  const scoreArray = score.split(``)
  const playersList = getPlayers(score)
  scoreArray.forEach((character) => {
    const keySave = character.toLowerCase()
    if (character === character.toUpperCase()) {
      playersList[keySave] = playersList[keySave] - 1
    }
    if (character === character.toLowerCase()) {
      playersList[keySave] = playersList[keySave] + 1
    }
  })
  const sorted = sortObject(playersList)
  return sorted
}

console.log(scoreCalc(ourScore))

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

;; ECL 12.12.1
;; $ ecl -shell tally.lisp
;; Tuesday, May 15th, 2018

(setq *players* (concatenate 'list "abcde"))
(setq *scores* nil)

(defun tally (ch str) (cons (- (count ch str) (count (char-upcase ch) str)) ch))
(defun sort-alist (a b) (> (car a) (car b)))

(defun main (str) (progn (setq *scores* nil)
			 (dolist (x *players*) (setq *scores* (cons (tally x str) *scores*)))
			 (setq *scores* (sort *scores* #'sort-alist))
	    	 (format t "~A~&" *scores*)))

(main "abcde")
(main "dbbaCEDbdAacCEAadcB")
(main "EbAAdbBEaBaaBBdAccbeebaec")

(quit)

Output:

$ ecl -shell tally.lisp
((1 . e) (1 . d) (1 . c) (1 . b) (1 . a))
((2 . d) (2 . b) (1 . a) (0 . c) (-2 . e))
((3 . c) (2 . d) (1 . e) (1 . a) (0 . b))

Small changes https://pastebin.com/2BSMhFu9

1

u/[deleted] May 15 '18

Python 3

def tally(scores):
    t = dict()
    for s in scores:
	    l = s.lower()
	    if l not in t:
		    t[l] = 0
	    if l == s:
		    t[l] += 1
	    else:
		    t[l] -= 1
    st = sorted(t, key=lambda l: t[l], reverse=True)
    return [(l, t[l]) for l in st]

1

u/edixon653 May 16 '18

Python 3 - Simple and readable

import operator

scores = {}
startingString = input(':')

for ch in startingString:
    if ch.islower(): mod = 1
    elif ch.isupper(): mod = -1
    else: mod = None

    if mod != None:
        if ch.lower() in scores.keys():
            scores[ch.lower()] += mod
        else: scores.update({ch.lower():mod})
    
sortedScores = sorted(scores.items(), key=operator.itemgetter(1))
sortedScores.reverse()
print(sortedScores)

1

u/[deleted] May 16 '18

F# uses the same collapse function that I always find myself using in these challenges. Feedback welcome.

open System

module Array =
    let collapse(kv:('a*'b)[]) =
        let data1, _ = kv |> Array.unzip
        let keys = data1 |> Array.distinct
        keys
        |> Array.map (fun x -> 
            (x, kv 
                |> Array.filter (fun (k,_) -> k=x) 
                |> Array.map snd))
let tally (str:string) =
    
    str.ToCharArray()
    |> Array.countBy id
    |> Array.map (fun (a,b) -> if Char.IsUpper(a) then (a,-b) else (Char.ToUpper(a),b))
    |> Array.collapse
    |> Array.map (fun (a,b) -> (a,b|>Array.sum))
    |> Array.sortByDescending snd

[|"abcde";"dbbaCEDbdAacCEAadcB";"EbAAdbBEaBaaBBdAccbeebaec"|]
|> Array.map tally
|> Array.iter (printfn "%A")

Output:

[|('A', 1); ('B', 1); ('C', 1); ('D', 1); ('E', 1)|]
[|('D', 2); ('B', 2); ('A', 1); ('C', 0); ('E', -2)|]
[|('C', 3); ('D', 2); ('E', 1); ('A', 1); ('B', 0)|]

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

use std::collections::HashMap;

fn main() {
    let input="dbbaCEDbdAacCEAadcB";
    let mut tallymap: HashMap<String, i32> = HashMap::new();

    for c in input.chars() {

        if c.is_uppercase() {
            let count = tallymap.entry(c.to_lowercase().to_string()).or_insert(0);
            *count -= 1;
        }

        if c.is_lowercase() {
            let count = tallymap.entry(c.to_string()).or_insert(0);
            *count += 1;
        }
    }
}

1

u/arthurelamothe May 16 '18 edited May 16 '18

C++

    #include <algorithm>
    #include <string>
    #include <iostream>
    #include <vector>
    #include <boost/foreach.hpp>

    typedef std::vector< std::pair< int, char > > vec_t;
    bool greater( const vec_t::value_type& c1, const vec_t::value_type& c2 ) { return c1.first > c2.first; }
    int main()
    {
        vec_t results;
        std::string s = "EbAAdbBEaBaaBBdAccbeebaec";
        for( char player = 'a'; player < 'f'; player++ ) {
            int score = 0;
            score =  std::count(s.begin(), s.end(), player) - std::count(s.begin(), s.end(), player - 32);
            results.push_back(std::make_pair(score, player));
        }
        std::sort(results.begin(), results.end(), greater);
        BOOST_FOREACH( const vec_t::value_type &v, results )
            std::cout << v.second<<":"<<v.first<<" ";
        return 0;
    }

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:

Sub GameScore()

Dim a, b, c, d, e, x, Score As Integer
Dim y As String

Range("a4").Value = "a"
Range("a5").Value = "b"
Range("a6").Value = "c"
Range("a7").Value = "d"
Range("a8").Value = "e"

x = 1

Do Until x = Len(Range("a1")) + 1
    y = Mid(Range("a1"), x, 1)
    If LCase(y) = y Then Score = 1 Else Score = -1
    
    If LCase(y) = "a" Then a = a + Score
    If LCase(y) = "b" Then b = b + Score
    If LCase(y) = "c" Then c = c + Score
    If LCase(y) = "d" Then d = d + Score
    If LCase(y) = "e" Then e = e + Score

    x = x + 1
Loop

Range("b4").Value = a
Range("b5").Value = b
Range("b6").Value = c
Range("b7").Value = d
Range("b8").Value = e

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B3"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A4:B8")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

So I decided to go for a dictionary approach instead:

Sub GameScores()

Dim Scores As New Scripting.Dictionary
Dim i As Variant
Dim j, x, z As Integer
Dim y As String

x = 1

Do Until x = Len(Range("a1")) + 1
    y = Mid(Range("A1"), x, 1)
    If LCase(y) = y Then z = 1 Else z = -1

    If Scores.Exists(LCase(y)) Then
        Scores(LCase(y)) = Scores(LCase(y)) + z
    Else
        Scores.Add LCase(y), z
    End If

    x = x + 1

Loop

ReDim arr(0 To Scores.Count - 1, 0 To 1)

For i = 0 To Scores.Count - 1
    arr(i, 0) = Scores.Keys(i)
    arr(i, 1) = Scores.Items(i)
Next i

For i = LBound(arr, 1) To UBound(arr, 1) - 1
    For j = i + 1 To UBound(arr, 1)
        If arr(i, 1) < arr(j, 1) Then
            Temp1 = arr(j, 0)
            Temp2 = arr(j, 1)
            arr(j, 0) = arr(i, 0)
            arr(j, 1) = arr(i, 1)
            arr(i, 0) = Temp1
            arr(i, 1) = Temp2
        End If
    Next j
Next i

Scores.RemoveAll

For i = LBound(arr, 1) To UBound(arr, 1)
    Scores.Add Key:=arr(i, 0), Item:=arr(i, 1)
Next i

For Each i In Scores
    Debug.Print i; Scores(i)
Next i

End Sub

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.

def tally(str):
    points = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}
    for char in str:
        points[char.lower()] += 1 if char.lower() == char else -1
    people = sorted([f"{person}: {point}" for person, point in points.items()], key = lambda x: -int(x[x.index(":") + 2:]))
    return ", ".join(people)
→ More replies (2)

1

u/ruineroflife May 17 '18

C# Compiled in Mono, should work in .net too anyways

    public class Tally
    {
        private static readonly List<string> Players = new List<string> { "a", "b", "c", "d", "e"};
        
        public static void TallyUp(string input)
        {
            var result = new Dictionary<string, int>();
            
            //initialize the players in our dictionary
            foreach (var player in Players)
                result.Add(player.ToLower(), 0);
            
            //then tally up our results
            foreach (var character in input)
                result[character.ToString().ToLower()] = result[character.ToString().ToLower()] + ((char.IsUpper(character)) ? -1 : 1);
            
            Console.WriteLine($"Results for... {input}");
            foreach (var item in result.OrderByDescending(x=>x.Value))
                Console.WriteLine($"{item.Key}:{item.Value.ToString()}");
        }
  }

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

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

const int CHAR_START_U = 'A';
const int CHAR_START_L = 'a';

typedef struct {
    char id; 
    int score;
} player;

int cmpFunc(const void * a, const void * b){ 
    int l = ((player *)a)->score;
    int r = ((player *)b)->score;
    return (r-l);
}

int main(int argc, char * argv[]){
    if(argc < 2){
        printf("Please provide a score to count.\n");
        return -1; 
    }   

    char * inputStr = argv[1];
    player players[5]; 

    //populate players with ids ranging from a to e
    for(int i = 0; i < 5; i++){
        players[i].id = 'a'+i;
        players[i].score = 0;
    }

    //use the alphabet and offset to access array indices
    for(int i = 0; i < strlen(inputStr); i++){
        if(isupper(inputStr[i])){
            players[inputStr[i] - CHAR_START_U].score--;
        } else {
            players[inputStr[i]- CHAR_START_L].score++;
        }   
    }   
    qsort(players, 5, sizeof(player), cmpFunc); 

    for(int i = 0; i < 5; i++){
        printf("| %c:%d ", players[i].id, players[i].score);
    }   
    printf("\n");
}

1

u/saladdressing May 17 '18

Javascript:

Feedback appreciated!

function tally(input) {

let inputArr = Array.from(input);
let players = [];
inputArr.forEach(ltr=>{
    let letter = ltr;
    let playObj = players.filter(item=>{
        return item.key === letter.toLowerCase();
    });
    if (playObj.length > 0) {
        if ((letter).charCodeAt(0) > 96) {
            playObj[0].score++;
        } else {
            playObj[0].score--;
        }
    } else {
        players.push({
            key: letter.toLowerCase(),
            score: (letter.charCodeAt(0) > 96) ? 1 : -1,
            toString(){
                console.log(this.key + ": " + this.score);
            }
        });
    }
});
players.sort((a, b)=>{
    if (a.score === b.score) return 1;
    return b.score - a.score;
}).toString();
}

tally("EbAAdbBEaBaaBBdAccbeebaec");

Output:

c: 3
d: 2
e: 1
a: 1 
b: 0

1

u/Porterhouse21 May 17 '18

VBA

Option Explicit

Sub LoopString()
    Dim i As Integer
    Dim myString As String
    Dim A As Range, B As Range, C As Range, D As Range, E As Range
    
    Set A = Cells(2, 2)
    Set B = Cells(3, 2)
    Set C = Cells(4, 2)
    Set D = Cells(5, 2)
    Set E = Cells(6, 2)
    
    A.Value = 0
    B.Value = 0
    C.Value = 0
    D.Value = 0
    E.Value = 0
    
    myString = Cells(1, 2).Value
    
    For i = 1 To Len(myString)
        Select Case Mid(myString, i, 1)
            Case Is = "a"
                A.Value = A.Value + 1
            Case Is = "b"
                B.Value = B.Value + 1
            Case Is = "c"
                C.Value = C.Value + 1
            Case Is = "d"
                D.Value = D.Value + 1
            Case Is = "e"
                E.Value = E.Value + 1
            Case Is = "A"
                A.Value = A.Value - 1
            Case Is = "B"
                B.Value = B.Value - 1
            Case Is = "C"
                C.Value = C.Value - 1
            Case Is = "D"
                D.Value = D.Value - 1
            Case Is = "E"
                E.Value = E.Value - 1
        End Select
    Next i
    Range("A2:B6").Sort Key1:=Range("B2:B6"), 
    Order1:=xlDescending
End Sub

Output:

A      3
D      2
E      1
B      0
C     -1

1

u/bebestversionofyou May 17 '18

Javascript

function tallyProgram(score){

    let tally = {};

    for(let i = 0; i < score.length; i++){

        let lower = score[i].toLowerCase(),
            point = tally[lower] || 0;

        tally[lower] = score.charCodeAt(i) >= 97 ? point + 1 : point - 1;

    }

    let rank = Object.keys(tally).sort((a, b) => tally[b] - tally[a] );

    return rank.map((player, i) => player + ":" + tally[player]).join(', ');

}

Input

"EbAAdbBEaBaaBBdAccbeebaec"

Output

c:3, d:2, e:1, a:1, b:0

1

u/DerpinDementia May 17 '18

Python 3

string = input("Enter scores >>> ")
print({score.lower() : string.count(score.lower()) - string.count(score.upper()) for score in string})

Challenge

Enter scores >>> EbAAdbBEaBaaBBdAccbeebaec
{'e': 1, 'b': 0, 'a': 1, 'd': 2, 'c': 3}

I was a bit unsure how to simplify this further for fun. All feedback welcome!

1

u/TacticalBastard May 18 '18

C

#include <stdio.h>

int main(){

int baseCap = 'A'; //Getting the lowest possible capital
int baseLow = 'a'; //Getting the lowest possible lowercase
int topCap = 'E'; //Getting the highest possible capital
int topLow = 'e'; //Getting the highest possible lowercase

char word[2048]; //Getting the score

char players[5] = {'a','b','c','d','e'};
int scores[5] = {0,0,0,0,0}; //Starting them off at 0

scanf("%s", word);

int i = 0;
for(i = 0; word[i] != '\0'; i++){
	if(word[i] >= baseCap && word[i] <= topCap)
		scores[word[i]-baseCap]--;
	else if(word[i] >= baseLow && word[i] <= topLow)
		scores[word[i]-baseLow]++;
	else
		printf("Invalid Input %c, Skipped\n", word[i]);
}

//Put in order
int j = 0;
for(i = 0; i < 5; i++){
	for(j = 0; j < 5-i-1; j++){
		if(scores[j] < scores[j+1]){
			char playerTemp = players[j];
			int scoreTemp = scores[j];
			players[j] = players[j+1];
			scores[j] = scores[j+1];
			players[j+1] = playerTemp;
			scores[j+1] = scoreTemp;
		}
	}
}

for(i=0; i < 5; i++ ){
	printf("%c: %d,",players[i],scores[i]);
}
printf("\n");

}

Any and all critique is welcome

1

u/pjury May 18 '18

Python3 Included a regex to verify the input.

def return_score(tally):
    score = {"a": 0, "b": 0, "c": 0, "d": 0, "e": 0}
    regexp = re.compile(r'^[a-eA-E]+$')
    if regexp.search(tally):
        for i in tally:
            if i.islower():
                score[i.lower()] += 1
            else:
                score[i.lower()] -= 1
        return score
    else:
        raise ValueError('Tally can only include letters A-E and a-e')

1

u/Daanvdk 1 0 May 18 '18

C

#include <stdio.h>

#define BUFFER_SIZE 2048
#define ALPHABET_SIZE 5

#define CHAR_INDEX(c) (((c) - 'A') % 32)
#define CHAR_VALUE(c) ((((c) - 'A') / 32) * 2 - 1)

int main() {
    // get input
    char input[BUFFER_SIZE];
    scanf("%s", input);

    // count tallies
    int tallies[ALPHABET_SIZE] = {0};
    for (int i = 0; input[i] != 0; i++) {
        tallies[CHAR_INDEX(input[i])] += CHAR_VALUE(input[i]);
    }

    // order tallies
    int ordered[ALPHABET_SIZE];
    for (int i = 0; i < ALPHABET_SIZE; i++) {
        ordered[i] = i;
        int j = i;
        while (j > 0 && tallies[ordered[j]] > tallies[ordered[j - 1]]) {
            ordered[j] = ordered[j - 1];
            ordered[--j] = i;
        }
    }

    // print output
    for (int i = 0; i < ALPHABET_SIZE; i++) {
        if (i > 0) {
            printf(", ");
        }
        printf("%c: %d", 'a' + ordered[i], tallies[ordered[i]]);
    }
    printf("\n");
}

1

u/primaryobjects May 18 '18

R

Gist | Demo

inputs <- c('dbbaCEDbdAacCEAadcB', 'EbAAdbBEaBaaBBdAccbeebaec')

tally <- function(input) {
  letters <- unlist(strsplit(input, ''))
  hash <- new.env()
  
  # Tally scores.
  sapply(letters, function(letter) {
    # If the letter is not uppercase it's a score. Otherwise, it's a loss.
    score <- ifelse(gregexpr("[A-Z]", letter) < 1, 1, -1)
    
    letter <- tolower(letter)
    hash[[letter]] <- ifelse(is.null(hash[[letter]]), score, hash[[letter]] + score)
  })
  
  # Get score values.
  scores <- c()
  keys <- ls(hash)
  scores <- t(sapply(keys, function(key) {
    c(scores, c(key, hash[[key]]))
  }))
  colnames(scores) <- c('player', 'score')
  scores <- as.data.frame(scores)
  scores$score <- as.numeric(as.character(scores$score))
  
  # Sort the scores.
  scores[order(scores$score, decreasing=T),]
}

format <- function(scores) {
  str <- sapply(1:nrow(scores), function(i) {
    row <- scores[i,]
    paste0(row$player, ':', row$score)
  })
  
  str
}

# Tally and print the scores for each input.
sapply(inputs, function(input) {
  scores <- format(tally(input))
  print(paste(scores, collapse=', '))
})

Output

"b:2, d:2, a:1, c:0, e:-2"
"c:3, d:2, a:1, e:1, b:0"

1

u/govomevo May 19 '18 edited May 19 '18

SQL (Oracle):

select lower(chr) player, sum(score) from (
  select chr, decode(chr,lower(chr),1,-1) score from (
    select substr(str,level,1) chr from (
      select 'EbAAdbBEaBaaBBdAccbeebaec' str from dual
    ) connect by level <= length(str)
  )
) group by lower(chr) order by sum(score) desc, player;

output:

PLAYER SUM(SCORE)

c 3

d 2

a 1

e 1

b 0

1

u/borislavvv May 19 '18

Python 3.6

def tally_program(inpt = 'dbbaCEDbdAacCEAadcB'): 
    players = {'a': 0, 'b': 0, 'c': 0, 'd':0, 'e':0}
    for c in inpt:
        if c in players:
            players[c] += 1
        elif c.isupper() and (c.lower() in players):
            players[c.lower()] -= 1

    return [(k, players[k]) for k in sorted(players, key=players.get, reverse=True)]

Output:

[('b', 2), ('d', 2), ('a', 1), ('c', 0), ('e', -2)]

1

u/darknes1234 May 19 '18

C++

#include <iostream>

const char* input1 = "abcde";
const char* input2 = "dbbaCEDbdAacCEAadcB";
const char* input3 = "EbAAdbBEaBaaBBdAccbeebaec";

int scores[] = { 0,0,0,0,0 };
int friends[] = { 'a','b','c','d','e' };

void swap(int *x, int *y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}

void selection_sort()
{
	int i, j, max_idx;

	for (i = 0; i < 4; i++) {
		max_idx = i;
		for (j = i + 1; j < 5; j++)
			if (scores[j] > scores[max_idx])
				max_idx = j;
		swap(&scores[max_idx], &scores[i]);
		swap(&friends[max_idx], &friends[i]);
	}
}

void to_score(char c)
{
	if (c >= 'A' && c <= 'Z')
		scores[c - 'A']--;
	else if (c >= 'a' && c <= 'z')
		scores[c - 'a']++;
}

int main()
{
	char c;
	for (int i = 0; (c = input3[i]) != '\0'; i++)
		to_score(c);

	selection_sort();

	for (int i = 0; i < 5; i++) {
		std::cout << (char)friends[i] << ":" << scores[i];
		if (i < 4)
			std::cout << ", ";
	}
	std::cout << std::endl;

	system("pause");
	return 0;
}

1

u/throwing_tantra May 20 '18

python3 one-liner, but it omits players with a negative score:

from collections import Counter
text = 'dbbaCEDbdAacCEAadcB' 
print((Counter(filter(str.islower, text)) - Counter(filter(str.isupper, text))).most_common()) 
[('d', 3), ('b', 3), ('a', 3), ('c', 2)]

1

u/throwing_tantra May 20 '18

trying some C++:

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>

bool cmp(const std::pair<char, int> &p1, const std::pair<char,int> &p2){
    return p1.second > p2.second;
}

int main() {
    std::string instr;
    getline(std::cin, instr);
    std::unordered_map<char,int> scorecounts;
    std::cout << instr << std::endl;
    for(unsigned int i=0; i< instr.length(); ++i){
        int score = -1;
        if( islower(instr[i])){
            score = 1;
        }
        char letter = tolower(instr[i]);
        if(scorecounts.count(letter) > 0) {
            score += scorecounts.find(letter)->second;
            scorecounts.erase(letter);
        }
        scorecounts.emplace(letter, score);
    }
    std::vector<std::pair<char,int> > v;
    std::copy(scorecounts.begin(), scorecounts.end(), back_inserter(v));
    std::sort(v.begin(), v.end(), cmp);

    for (auto i = v.begin(); i != v.end(); ++i) {
        std::cout << i->first << " : " << i->second << "\n";
    }
}

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!

func get_scores(points):
var score_keeper = {}

# Fairly self explanatory
for point in points:
	if not point.to_lower() in score_keeper:
		score_keeper[point.to_lower()] = 0
for point in points:
	if point == point.to_upper():
		score_keeper[point.to_lower()] -= 1
	else:
		score_keeper[point.to_lower()] += 1

# Duplicating a dict to iterate over and an array to order how things are printed
var comparison_copy = score_keeper.duplicate()
var order_to_call = []

# 1100% sure this could be a lot better
    # Edit: There is a sort keyword in it but only for arrays. Dicts can return arrays but I wouldn't be able to match keys
for person in score_keeper:
	var current_key = comparison_copy.keys()[0]
	for key in comparison_copy.keys():
		if comparison_copy[key] >= comparison_copy[current_key]:
			current_key = key
	order_to_call.append(current_key)
	comparison_copy.erase(current_key)

for key in order_to_call:
	print(key, ':', score_keeper[key])

result: 

c:3
d:2
a:1
e:1
b:0

1

u/nitishc May 21 '18

Rust

use std::collections::HashMap;
use std::io;

fn main() {
    //Read input
    let mut s = String::new();
    io::stdin().read_line(&mut s).expect("Failed to read input");
    s = s.trim().to_string();

    let mut h = HashMap::new();
    for c in s.chars() {
        let lower = c.to_lowercase().next().unwrap();
        if c.is_lowercase() {
            h.entry(lower).and_modify(|c| *c += 1).or_insert(1);
        } else {
            h.entry(lower).and_modify(|c| *c -= 1).or_insert(-1);
        }
    }
    let mut seq = h.iter().collect::<Vec<_>>();
    seq.sort_by(|a, b| (b.1).cmp(a.1));
    println!("{:?}", seq);
}

1

u/minimim May 21 '18 edited Jun 04 '18

Perl 6

#!/usr/bin/env perl6

use v6.c;

grammar Scores-grammar { token TOP { [<lower>|<upper>]+ } }

class Scores-actions {
    has Hash $.tally;

    method lower ($/) { ++$!tally{fc $/} }
    method upper ($/) { --$!tally{fc $/} }
}

#| Gives the resulting score from highest to lowest.
sub MAIN (
    Str $scoreboard where /^[<.lower>|<.upper>]+$/
        #=„A series of characters indicating who scored a point.
         First letter of player's name in lower case to win a point.
         Upper case to lose a point.”
) {
    my Scores-actions $actions .= new;
    parse(Scores-grammar: $scoreboard, :$actions);
    put $actions.tally.sort({-.value, .key}).map({"$_.key():$_.value()"}).join(", ");
}

OUTPUT:

$ tally abcde
a:1, b:1, c:1, d:1, e:1
$ tally dbbaCEDbdAacCEAadcB
b:2, d:2, a:1, c:0, e:-2
$ tally EbAAdbBEaBaaBBdAccbeebaec
c:3, d:2, a:1, e:1, b:0
$ tally åéÅ
é:1, å:0
$ tally
Usage:
  tally <scoreboard> -- Gives the resulting score from highest to lowest.
  
<scoreboard>    A series of characters indicating who scored a point. First letter of player's name in lower case to win a point. Upper case to lose a point.
$ tally 16
Usage:
  tally <scoreboard> -- Gives the resulting score from highest to lowest.
  
<scoreboard>    A series of characters indicating who scored a point. First letter of player's name in lower case to win a point. Upper case to lose a point.

Try it online!

I like feedback.

2

u/ogniloud Jun 04 '18 edited Jun 04 '18

Sorry for the late reply! I'm really surprised with your use of grammars; they are truly powerful. I'm just getting my feet wet with them (just started reading Think Perl 6). Aside from the docs, do you recommend any additional resources to learn more about them?

I think if perl6 is in your path, you just need to use use v6 or use v6.c to specify the version if more than one.

→ More replies (4)

1

u/Vinas94 May 21 '18

Solution in R:

scores <- "EbAAdbBEaBaaBBdAccbeebaec" %>%
  strsplit(., "") %>%
  table(.) %>%
  as.data.frame(.) %>%
  setNames(c("Player", "Score")) %>%
  mutate( Score = ifelse(Player%in%LETTERS, -Score, Score),
         Player = tolower(Player)) %>%
  group_by(Player) %>%
  summarise(Score = sum(Score)) %>%
  print(.)

1

u/ReturnValueOther May 21 '18

JavaScript

const challengeInput = 'EbAAdbBEaBaaBBdAccbeebaec';
const inputArray = challengeInput.split("");

const scores = inputArray
  // First, tally up the scores, saving as subarrays (to sort later)
  .reduce( (accum, char) => {
    const lowerCaseChar = char.toLowerCase()
    const scoreToAdd = char === lowerCaseChar ? 1 : -1;
    const charPosition = accum.findIndex( (e) => e[0] === lowerCaseChar);
    if (charPosition >= 0) {
      accum[charPosition][1] += scoreToAdd;
    } else {
      accum.push([lowerCaseChar, scoreToAdd])
    }
    return accum;
  }, [])
  // Then, sort subarrays (based on value, which is index 1 in each subarray) largest to smallest
  .sort( (a, b) => {
      return a[1] > b[1] ? -1 : 1;
    })
  // Then, render a string to output the results
  .reduce( (accum, curVal, i, arr) => {
    accum += `${curVal[0]}: ${curVal[1]}${i + 1 < arr.length ? ', ' : ''}`;
    return accum;
    }, '');

console.log(scores);
// Output: c: 3, d: 2, a: 1, e: 1, b: 0

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

{-# LANGUAGE TupleSections #-}
module Tally where

import           Data.Bifunctor (bimap)
import           Data.Char      (isLower, toLower)
import           Data.Function  (on)
import           Data.List      (nubBy)
import           Data.Maybe     (fromMaybe)

main :: IO ()
main = interact $ unlines . map (show . tally) . lines

tally :: String -> [(Char,Int)]
tally = nubBy ((==) `on` fst) . foldr lkup [] where
  lkup :: Char -> [(Char,Int)] -> [(Char,Int)]
  lkup ch m = (:m) . fromMaybe (f (ch,0)) $ f . (ch, ) <$> lookup (toLower ch) m 
    where f = bimap toLower $ if isLower ch then (1+) else subtract 1

1

u/Endlessdex May 21 '18

Python 3:

I wanted to try using the Counter structure instead of basic lists/dicts.

def c361(input):
    scores = Counter(input.translate(str.maketrans('', '', ascii_uppercase)))
    neg = Counter(input.translate(str.maketrans(ascii_uppercase, ascii_lowercase, ascii_lowercase)))
    scores.subtract(neg)
    return scores.most_common()

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

#include <iostream>
#include <map>
#include <ctype.h>
#include <vector>
#include <algorithm>    

using namespace std;    

const char* input1 = "abcde";
const char* input2 = "dbbaCEDbdAacCEAadcB";
const char* input3 = "EbAAdbBEaBaaBBdAccbeebaec";    

typedef pair<char,int> score;    

void tally(const char* input){
	cout << "input was: " << input << endl;
	string s(input);
	map<char,int> counts;
	vector<score> v;		//for sorting    

	for(auto c : s) counts[tolower(c)] += (islower(c)) ? 1 : -1;    

	//copy k-v pairs from map to iterator
	copy(counts.begin(),counts.end(), back_inserter<vector<score>>(v));    

	//sort by letter order if not counts are equal
	sort(v.begin(), v.end(),
		[](const score& l, const score& r){
			if(l.second != r.second) return l.second > r.second;
			return l.first < r.first;
		});    

	cout << "output: " << endl;
	for(auto it : v){
		cout << it.first << ":" << it.second;
		if(it != v.back()) cout << ", ";
	}
	cout << endl << endl;    

}    

int main(int argc, char* argv[]){
	tally(input1);
	tally(input2);
	tally(input3);
}

Output:

input was: abcde
output:
a:1, b:1, c:1, d:1, e:1

input was: dbbaCEDbdAacCEAadcB
output:
b:2, d:2, a:1, c:0, e:-2

input was: EbAAdbBEaBaaBBdAccbeebaec
output:
c:3, d:2, a:1, e:1, d:0

EDIT: added output and formatting

1

u/Icenomad May 22 '18

Ruby

def tally_points (string)
  hash_of_players = Hash.new(0)
  array= string.chars
  array.each do |char|
    if char == char.downcase
      hash_of_players[char.downcase] += 1
    else
      hash_of_players[char.downcase] -= 1
    end
  end
  
 return hash_of_players.sort_by{|k,v| v}
end

1

u/greenguff May 22 '18

perl

Feedback? (It's word-y, I know :/)

#!/usr/bin/env perl

use strict;
use warnings;

my $score_str = 'EbAAdbBEaBaaBBdAccbeebaec';

my $players = {
    'a' => 0,
    'b' => 0,
    'c' => 0,
    'd' => 0,
    'e' => 0,
};

my @scores = split('',$str);

foreach my $score (@scores) {
    my $player = lc($score);
    my $point = $score =~ m/[A-Z]/ ? -1 : 1;
    $players->{$player} += $point;
}

foreach my $player (sort {$players->{$a} <=> $players->{$b}} keys %$players) {
    print "$player: $players->{$player}";
}

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.

function TallyProgramm(string)

    points = ['a' 'A' 'b' 'B' 'c' 'C' 'd' 'D' 'e' 'E'];
    res = [0,0,0,0,0];

    for i = 1: length(string)
	    for k = 1 : length(res)
		    if string[i] == points[2k-1]
			    res[k] = res[k] + 1;
		    elseif string[i] == points[2k]
			    res[k] = res[k] - 1;
		    end
	    end
    end

    for i = 1: length(res)
	    print("Player ",points[2i-1]," : ",res[i],"\n")
    end
end

TallyProgramm("dbbaCEDbdAacCEAadcB")

Output:

Player a : 1
Player b : 2
Player c : 0
Player d : 2
Player e : -2

1

u/throwing_tantra May 23 '18

Java

import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        HashMap<Character, Integer> hm = new HashMap<Character, Integer>();

        for (char ch: sc.nextLine().toCharArray()){
            char lowered = Character.toLowerCase(ch);
            int charval = -1;
            if(lowered==ch){
                charval = 1;
            }
            hm.put(lowered, hm.getOrDefault(lowered, 0) + charval);
        }

        hm.entrySet()
                .stream()
                .sorted(
                        Collections.reverseOrder(
                                HashMap.Entry.comparingByValue()))
                .forEach(item ->
                        System.out.println(item));
    }
}

1

u/eternalblue227 May 23 '18 edited May 23 '18

Java Feedback would be appreciated.

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class TallyMain {
	static final int NO_OF_CHARS = 256;
	public TallyMain() {
		Scanner s = new Scanner(System.in);
		this.getScores(s.nextLine());
		s.close();
	}
	public void getScores(String str) {
		HashMap<Character, Integer> scores = new HashMap<>();
		int[] count =  new int[NO_OF_CHARS];
		char[] tallies = str.toCharArray();
		int i;
		for(i=0;i<tallies.length;i++) {
			count[(int)tallies[i]]++;
		}
		for(i=65;i<91;i++) {
			if(count[i]!=0||count[(i+32)]!=0)
				scores.put((char)(i+32),count[(i+32)]-count[i]);
		}
		this.sortMapbyValue(scores);
	}
	public void sortMapbyValue(HashMap<Character, Integer> map){
		ArrayList<Character> keys = new ArrayList<>(map.keySet());
		ArrayList<Integer> values = new ArrayList<>(map.values());
		Collections.sort(values,Collections.reverseOrder());
		LinkedHashMap<Character, Integer> sortedMap =  new LinkedHashMap<>();
		for(int i : values) {
			for(char c:keys) {
				if(map.get(c)==i && !sortedMap.containsKey(c)) {
					sortedMap.put(c, i);
				}
			}
		}
		System.out.println(sortedMap);
	}
	public static void main(String[] args) {
		new TallyMain();
	}
}	

Edit: Its my first submission on this sub.

1

u/equation_x May 23 '18

Python 3.4.2

def easy_361(friends_scores):
    friends = {'a': 0, 'b': 0, 'c': 0, 'd':0, 'e':0}
    friends_list = [friends_scores[i] for i in range(len(friends_scores))]
    bad_friends = ['A','B','C','D','E']

    for score in friends_list:
	    if score in friends:
		    friends[score] += 1
	    elif score in bad_friends:
		    friends[score.lower()] -= 1

    return(sorted(friends.items(), key=lambda x: x[1],reverse=True))

friends_scores = input()
print(easy_361(friends_scores))

1

u/eesov May 23 '18

Python

def score(seq):
    d = {player: seq.count(player.lower()) - seq.count(player.upper())
         for player in set(seq.lower())}
    return sorted(d.items(), key=lambda x: -x[1])


test_data = ['abcde', 'dbbaCEDbdAacCEAadcB',
             'EbAAdbBEaBaaBBdAccbeebaec', 'asdDDkdlaDDxa']
for test in test_data:
    print(score(test))

>>>
[('a', 1), ('c', 1), ('b', 1), ('e', 1), ('d', 1)]
[('b', 2), ('d', 2), ('a', 1), ('c', 0), ('e', -2)]
[('c', 3), ('d', 2), ('a', 1), ('e', 1), ('b', 0)]
[('a', 3), ('k', 1), ('l', 1), ('s', 1), ('x', 1), ('d', -2)]
[Finished in 0.0s]

1

u/chcampb May 23 '18 edited May 23 '18

Semi cheating python version

val = """abcde
dbbaCEDbdAacCEAadcB"""
players, scores = val.split('\n')
print(sorted({k:(scores.count(k) - scores.count(k.upper())) for k in players}.items(),key=lambda x:x[1], reverse=True))
→ More replies (2)

1

u/jpusztay May 23 '18

Python

Made a Terminal Interface so the code can be reusable for the five players.

def losePoint(char, score_d):
    if char == char.upper():
        score_d[char.lower()] = score_d.get(char.lower(),int) - 1

def gainPoint(char, score_d):
    if char == char.lower():
        score_d[char.lower()] = score_d.get(char.lower(),int) + 1

def tally(char_series):
    score_d = dict()
    for player in 'abcde':
        score_d[player] = 0
    for char in char_series:
        losePoint(char,score_d)
        gainPoint(char,score_d)
    return score_d

def main():
    char_series = str(input("Series of characters: "))
    print(tally(char_series))

if __name__ == "__main__":
    main()

1

u/tk854 May 24 '18

Java

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Main {

	static HashMap tally(String input) {
		
		HashMap<Character, Integer>  scores = new HashMap<Character, Integer>();
		
		
		for(Character c : input.toCharArray()) {
			
			Character player = Character.toLowerCase(c);
			int point = (Character.isLowerCase(c) ? -1 : 1);
			
			if(!scores.containsKey(player)) {
				scores.put(Character.toLowerCase(c), point);
			} else {
				scores.put(player, scores.get(player) + point);
			}
		}
		
		return scores;
	}
	
	public static void main(String[] args) {
		
		HashMap<Character, Integer> playerScores = tally("dbbaCEDbdAacCEAadcB");

		for (Map.Entry<Character, Integer> cursor : playerScores.entrySet()) {
			System.out.println(cursor.getKey() + ": " + cursor.getValue());
		}

	}

	
}

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:

class Player:
    def __init__(self, name: str):
        self.name = name
        self.score = 0

    def add_score(self, score):
        self.score += score
        
    def __str__(self):
        return f'{self.name} - {self.score}'

    def __repr__(self):
        return f'{self.name} - {self.score}'

    
class Game:
    def __init__(self):
        self.players = {}

    def add_player(self, name: str):
        self.players[name] = Player(name)

    def letter_to_point(self, letter: str):
        if letter.isupper():
            return -1
        else:
            return 1

    def score_string(self, scores: str):
        for letter in scores:
            if letter.lower() not in self.players:
                self.add_player(letter.lower())
            self.players[letter.lower()].add_score(self.letter_to_point(letter))

    def score_tally(self):
        value_dict = {}
        for key, value in self.players.items():
            value_dict[key] = value.score
        print('Player: Score')
        for key, value in sorted(value_dict.items(), key=lambda t: t[1], reverse=True):
            print(f'{key}: {value}')
            

def main():
    game = Game()
    game.score_string('EbAAdbBEaBaaBBdAccbeebaec')
    game.score_tally()

if __name__ == '__main__':
    main()

Output:

Player: Score
c: 3
d: 2
e: 1
a: 1
b: 0

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

    println("dbbaCEDbdAacCEAadcB".groupBy { it.toLowerCase() }.map { it.key to it.value.map { if (it.isLowerCase()) 1 else -1 }.sum() }.sortedByDescending { it.second })

1

u/BSISJ7 May 24 '18

Java

import java.util.*;

public class Tally{

	public static void main(String... args){
		String players = "abcde";
		String scores = "EbAAdbBEaBaaBBdAccbeebaec";
		HashMap<Character, Integer> playerMap = new HashMap<>();
		
		for(int x = 0; x < players.length(); x++){
			playerMap.put(players.charAt(x), 0);
		}
		
		for(int x = 0; x < scores.length(); x++){
			Character point = scores.charAt(x);
			Character player = Character.toLowerCase(point);
			Integer score = playerMap.get(player);
			
			if(Character.isUpperCase(point)){
				playerMap.put(player, score-1);
			}
			else{
				playerMap.put(player, score+1);
			}
		}
		
		Comparator<Character> valComparator = new ValueComparator<Character, Integer>(playerMap);
		TreeMap<Character, Integer> result = new TreeMap<Character, Integer>(valComparator);
		result.putAll(playerMap);
		System.out.println(result);
	}
}

class ValueComparator<K extends Comparable<K>, V extends Comparable<V>> implements Comparator<K>{
	HashMap<K, V> map = new HashMap<K, V>();
	
	public ValueComparator(HashMap<K, V> map){
		this.map.putAll(map);
	}
	
	@Override
	public int compare(K keyOne, K keyTwo){
		int compVal = -map.get(keyOne).compareTo(map.get(keyTwo));
		if(compVal != 0)
			return compVal;
		else
			return keyOne.compareTo(keyTwo);
	}
}

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.

proc sort_byval {hash {order "-decreasing"}} {
  return [lsort -stride 2 -index 1 $order $hash]
}

proc tally_scores {line} {
  foreach c [split $line {}] {
    dict incr score [string tolower $c] \
         [expr [string is upper $c] ? -1 : 1]
  }
  return $score
}

gets stdin line
while {$line != ""} {
  puts [sort_byval [tally_scores $line]]
  gets stdin line
}

Input

abcde
dbbaCEDbdAacCEAadcB
EbAAdbBEaBaaBBdAccbeebaec

Output

$ tclsh challenge_361.tcl < challenge_361-data
a 1 b 1 c 1 d 1 e 1
d 2 b 2 a 1 c 0 e -2
c 3 d 2 e 1 a 1 b 0

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.

proc parse_line {line} {
  for {set i 0} {$i < [string length $line]} {incr i} {
    set player [string index $line $i]
    if [string is upper $player] {
      incr scores([string tolower $player]) -1
    } else {
      incr scores($player)
    }
  }
  # Flattens the associative array and sorts it by value
  return [lsort -stride 2 -index 1 \
          -decreasing -integer [array get scores]]
}

1

u/chakkaveenu May 25 '18

Ruby

def tally(string)
  players = string.downcase.split("").uniq
  scores = players.map { |player| string.count(player) - string.count(player.upcase) }
  tally = scores.zip(players).sort.reverse
  tally.each {|t| print "#{t[1]}:#{t[0]},"}
end

1

u/FinalPerfectZero May 25 '18 edited May 25 '18

C#

    var orderedResults = "EbAAdbBEaBaaBBdAccbeebaec"
      .ToCharArray()
      .Select(c => c - 65)
      .GroupBy(c => c % 32, c => (c - (c % 32) - 16) / 16)
      .Select(g => new { g.Key, Total = g.Sum() })
      .OrderByDescending(g => g.Total)
      .Select(c => $"{(char)(c.Key + 97)}:{c.Total}");
    Console.Write(string.Join(", ", orderedResults));

Output: c:3, d:2, e:1, a:1, b:0

Explanation:

    var orderedResults = "EbAAdbBEaBaaBBdAccbeebaec"
      .ToCharArray() // Get an enumerator to loop over
      .Select(c => c - 65) // offset of (int)A - start of alphanumerics
      // since (a - A) == 32, mod by 32 so we group together upper/lower
      //  then get either 0 or 32, subtract 16, and divide by 16, for +1/-1
      .GroupBy(c => c % 32, c => (c - (c % 32) - 16) / 16) 
      .Select(g => new { g.Key, Total = g.Sum() })
      .OrderByDescending(g => g.Total)
      // Our key is the offset from the uppercase char at this point
      //  add 65 (A) then 32 (a - A)
      //  to get display value we want
      .Select(c => $"{(char)(c.Key + 97)}:{c.Total}");
    Console.Write(string.Join(", ", orderedResults));

1

u/marucOG May 26 '18

Python

def scores(string):
    scores = {chr(97 + i): 0 for i in range(5)}
    for char in string:
        scores[char.lower()] += -0.0625 * (ord(char.lower()) - ord(char)) + 1
    return scores

1

u/BlasphemousJoshua May 26 '18

Swift 4.1

let challengeInput = "EbAAdbBEaBaaBBdAccbeebaec"

func score(input: String) {
    // unique lowercase letters to track score
    let players: Set<Character> = Set(input.lowercased().map {$0} )
    // the score, initialized to zero
    var score: [Character: Int] = Dictionary(uniqueKeysWithValues:
        zip(players, repeatElement(0, count: players.count)) )
    // make loop to process one line of input
    for char in input {
        let lowerCased = String(char).lowercased().first!
        guard let currentScore = score[lowerCased]
            else { fatalError("score[lowerCased] returned nil")}
        score[lowerCased] = lowerCased == char ? currentScore + 1 : currentScore - 1
    }
    // sort dictionary by values first, then keys (the Character)
    let sortedDict = score.sorted(by: { (lTuple, rTuple) in
        let (lKey, lValue) = lTuple
        let (rKey, rValue) = rTuple
        guard lValue != rValue
            else { return lKey < rKey }
        return lValue > rValue
    })
    let output: String = sortedDict.map { (key, value) in
        return "\(key):\(value)" }
        .joined(separator: ", ")
    print(output)
}

score(input: challengeInput)

Output:

c:3, d:2, a:1, e:1, b:0

1

u/rebuked May 26 '18 edited May 26 '18

Haskell

import Data.Char(isUpper, isLower, toLower)

type Map a b = [(a, b)]

readInput :: String -> IO()
readInput input = print $ process input []

-- Update the count for each letter in the String
-- Once all letters in the String have been updated, sort the map 
process :: String -> (Map Char Int) -> (Map Char Int)
process [] map | isInDescOrder map = map
               | otherwise         = sort map []
process (x:xs) map | isLower x = process xs $ incrementCount x map []
                   | otherwise = process xs $ decrementCount (toLower x) map []

-- Increment the letter's count by 1. If it doesn't exist then initialise it
-- with a value of 1
incrementCount :: Char -> (Map Char Int) -> (Map Char Int) -> (Map Char Int)
incrementCount c [] seen = (c,1):seen
incrementCount c ((k,v):m) seen | c == k    = ((k,(v+1)):m) ++ seen
                                | otherwise = incrementCount c m ((k,v):seen)

-- Decrement the letter's count by 1. If it doesn't exist then create it with
-- a count of -1 
decrementCount :: Char -> (Map Char Int) -> (Map Char Int) -> (Map Char Int)
decrementCount c [] seen = (c,(-1)):seen
decrementCount c ((k,v):m) seen | c == k    = ((k,(v-1)):m) ++ seen
                                | otherwise = decrementCount c m ((k,v):seen)

-- Sort the map into descending order based on the count. This works by
-- recursively prepending the smallest element of the map, meaning the latest-
-- most prepended element is the largest (located at the front)
sort :: (Map Char Int) -> (Map Char Int) -> (Map Char Int)
sort [] sortedMap = sortedMap
sort map@((k,v):m) sortedMap = sort newMap (smallest:sortedMap)
                              where smallest = findSmallest map (k,v)
                                    newMap   = removeElement map smallest

-- Find the smallest element in the map by traversing through the map while
-- keeping track of the current smallest element found
findSmallest :: (Map Char Int) -> (Char, Int) -> (Char, Int)
findSmallest [] crtLowest = crtLowest
findSmallest ((k,v):m) crtLowest@(k2,v2) | v < v2    = findSmallest m (k,v)
                                         | otherwise = findSmallest m crtLowest

-- Remove the element in the map by filtering it out and then returning the
-- result
removeElement :: (Map Char Int) -> (Char, Int) -> (Map Char Int)
removeElement map (k,v) = filter (\(a,b) -> a /= k) map

-- Check if the map is descending order: first map it to a list of ints for
-- easier element access
isInDescOrder :: (Map Char Int) -> Bool
isInDescOrder oMap = checkOrder $ map(\(a,b) -> b) oMap

-- Then recursively compare the head with the next-most element throughout the
-- whole list
checkOrder :: [Int] -> Bool
checkOrder [] = True
checkOrder (x:xs) | length xs == 0 = True
                  | x == head xs   = checkOrder xs
                  | otherwise      = False

Challenge output:

[('c',3),('d',2),('e',1),('a',1),('b',0)]

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

public void tally(String s) {
    HashMap<Character, Integer> scores = new HashMap<>();
    for (int i = 0; i < s.length(); i++) {
        char a = s.toLowerCase().charAt(i);
        if (!scores.containsKey(a)) {
            scores.put(a, 0);
        }
        if (s.charAt(i) < 97)
            scores.put(a, scores.get(a) - 1);
        else
            scores.put(a, scores.get(a) + 1);
    }
    System.out.println(scores);
}

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!

#include <iostream>
#include <string>

using namespace std;

int main(int argv, char** argc)
{
    if (argv < 2) {
        cerr << "Usage: Input string of scores to process. " << endl;
        cerr << "EX - abCdabBc" << endl;
        return -1;
}

string tally = argc[1];         // tally string
int len = tally.length();       // length of string
cerr << tally << endl;

//Tableau of score tallies for each player
int scoreA = 0;
int scoreB = 0;
int scoreC = 0;
int scoreD = 0;
int scoreE = 0;

/*
 * Read string for score values
 * Lowercase letter: player gained a point
 * Uppercase letter: player lost a point
 */
for (std::string::iterator pos = tally.begin(); pos != tally.end(); ++pos) {
    if (*pos == 'a') {
        scoreA++;
    } else if (*pos == 'A') {
        scoreA--;
    } else if (*pos == 'b') {
        scoreB++;
    } else if (*pos == 'B') {
        scoreB--;
    } else if (*pos == 'c') {
        scoreC++;
    } else if (*pos == 'C') {
        scoreC--;
    } else if (*pos == 'd') {
        scoreD++;
    } else if (*pos == 'D') {
        scoreD--;
    } else if (*pos == 'e') {
        scoreE++;
    } else if (*pos == 'E') {
        scoreE--;
    } else {
        //Do nothing
    }
}

cout << "Scores" << endl;
cout << "a: " << scoreA << endl;
cout << "b: " << scoreB << endl;
cout << "c: " << scoreC << endl;
cout << "d: " << scoreD << endl;
cout << "e: " << scoreE << endl;
cout << endl;

return 0;

}

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.

chars = ['a', 'b', 'c', 'd', 'e']
vals = [0, 0, 0, 0, 0]

input = 'EbAAdbBEaBaaBBdAccbeebaec'

input.each_char do |char|
	factor = (char == char.upcase) ? -1 : 1
	vals[chars.index(char.downcase)] += factor
end

dict = Hash[chars.zip(vals)]
dict = Hash[dict.sort_by {|let, val| val}.reverse!]

puts dict

Outputs the following:

{"c"=>3, "d"=>2, "a"=>1, "e"=>1, "b"=>0}    

1

u/PlsNoPics May 27 '18

This is my first shared Code ever pls be gentle :)

My Code

score = [0,0,0,0,0]
possi = ["a", "A", "b", "B", "c", "C", "d", "D", "e", "E"]
inp   = "EbAAdbBEaBaaBBdAccbeebaec"
list  = []

options = { "a" : 0,                                       #the possible input options
            "A" : 1,
            "b" : 2,
            "B" : 3,
            "c" : 4,
            "C" : 5,
            "d" : 6,
            "D" : 7,
            "e" : 8,
            "E" : 9}

findic = {}                                                #the combined dictionary of player and score

i   = 0
let = 0
num = 0

while i < len(inp):                                        #calculate the score

    let = inp[int(i)]
    num = options[let]+ 2

    if num / 2 == int(num / 2):                            #if the number is even ...
        num -= 2
        score[int(num/2)] += 1
    else:                                                  #else if it's odd ...
        num -= 3        
        score[int(num/2)] -= 1
    i+=1

p = 0

while p < 10:                                              #combine players and scores in to a dictionary (i feel
    findic[possi[int(p)]] = score[int(p/2)]                #like its possible to improve my code here ¯_('-')_/¯)
    p+=2

list = sorted(findic.items(), key = lambda x:(-x[1],x[0])) #write the final dictionary into a list of tuples and sort it

k =0 
while k < 5:                                               #print the sorted tuples
    print(list[k][0], " : " ,list[k][1])
    k+=1

Output

c  :  3
d  :  2
a  :  1
e  :  1
b  :  0

Edit: fixed a wrong annotation comment

1

u/sfalkson May 27 '18

written using Python

input = raw_input("Enter your string: ")
lower_input = input.lower()

score_dict = {}

for i in lower_input:
	if i in score_dict:
		score_dict = score_dict
	else:
		score_dict[i] = 0
		
for i in input:
	if i == i.upper():
		score_dict[i.lower()] = score_dict[i.lower()] -1
	else:
		score_dict[i] = score_dict[i] + 1
		
for key, value in sorted(score_dict.iteritems(), key=lambda (k,v): (v,k), reverse=True):
	print ("%s: %s" %(key, value))

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.

import sys, string

scores = sys.argv[1]
playDict = {}

def plusminus(test):
    i = 0
    if test == True:
        i = 1
    else:
        i = -1
    return i

for char in scores:
    if char in (string.ascii_uppercase):
        add = False
    elif char in (string.ascii_lowercase):
        add = True
    else:
        print(char + ' is not an alphabetical character. Continuing.')
        continue

    key = char.lower()
    if key in playDict.keys():
        playDict[key] += plusminus(add)
    else:
        playDict[key] = plusminus(add)

print(playDict)

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

<?php

function validateInput($input, $scores)
{
    // checks to see if each character in input is in the
    // scores array as lowercase character, fails program if not
    for ($i = 0; $i < strlen($input); $i++) {
        if (!(array_key_exists(strtolower($input[$i]), $scores))) {
            exit('Invalid input.');
        }
    }
    return true;
}

function validateArgs($argc, $argv)
{
    if ($argc < 2) {
        exit("Supplied input is invalid.\n");
    }
    return $argv[1];
}

function main($argc, $argv)
{
    $input  = validateArgs($argc, $argv);
    $scores = [
        'a' => 0,
        'b' => 0,
        'c' => 0,
        'd' => 0,
        'e' => 0
    ];

    if (validateInput($input, $scores)) {
        // input safe, proceed
        for ($i = 0; $i < strlen($input); $i++) {
            if (ord($input[$i]) >= 65 && ord($input[$i]) <= 69) {
                // capital letter, decrease value
                $scores[strtolower($input[$i])] -= 1;
            } else {
                // lowercase letter, increase value
                $scores[strtolower($input[$i])] += 1;
            }
        }
    }

    // sort in descending order
    arsort($scores);

    // display output
    foreach ($scores as $key => $value) {
        echo "{$key}: {$value}\n";
    }
}

main($argc, $argv);

Input

php main.php dbbaCEDbdAacCEAadcB

Output

b: 2
d: 2
a: 1
c: 0
e: -2

1

u/involuntary_genius May 28 '18

I am trying to learn Ruby, so here is my attempt at the challenge.

CODE

scores = Hash.new(0)
input="EbAAdbBEaBaaBBdAccbeebaec"
input.each_char { |chr|
if chr.ord == chr.downcase.ord
  scores[chr] += 1
else
  scores[chr.downcase] -=1
end
}
scores=scores.sort_by{|k,v| -v}
scores.each{ |k,v|
puts k + ": " + v.to_s}

1

u/orduz May 29 '18

Python 3

from collections import defaultdict

def tally(events):
    scores = defaultdict(int)

    for e in events:
        scores[e.lower()] += 1 if e.islower() else -1

    return sorted([(k, v) for k,v in scores.items()], key=lambda x: x[1], reverse=True)

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

score_seq = "dbbaCEDbdAacCEAadcB"
scores_dict = defaultdict(int) #initialize default values for future keys, default value of int is 0

for score in score_seq:
    if score.isupper():
        scores_dict[score.lower()] -= 1
    else:
        scores_dict[score.lower()] += 1
#The method items() returns a list of dict's (key, value) tuple pairs
#itemgetter() to retrieve specific fields from a tuple record
print sorted(scores_dict.items(), key=itemgetter(1),reverse=True) 

1

u/nso95 May 29 '18

Here's my C# solution, I'd really appreciate feedback.

namespace friend_tally
{
    class TallyCounter
    {
        static public Dictionary<char, int> TallyPoints(string series) {

            Dictionary<char, int> scores = new Dictionary<char, int>() { { 'a', 0 }, { 'b', 0 }, { 'c', 0 }, { 'd', 0 }, { 'e', 0 } };

            foreach (char character in series)
            {
                if (char.IsLower(character) && scores.ContainsKey(character)) {
                    scores[character]++;
                }
                else if (char.IsLower(character) && !scores.ContainsKey(character))
                {
                    scores[character] = 1;
                }
                else if (char.IsUpper(character) && scores.ContainsKey(char.ToLower(character)))
                {
                   scores[char.ToLower(character)]--;
                } 
            }

            return scores;
        }



        static void Main(string[] args)
        {
            Dictionary<char, int> scores = TallyPoints("dbbaCEDbdAacCEAadcB");

            foreach (var score in scores.OrderByDescending(r => r.Value))
            {
                Console.Write("{0}: {1}, ", score.Key, score.Value);
            }

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

1

u/whoneedsintellij May 30 '18 edited May 30 '18

Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

class E361 {

    public static void main(String[] args) {
        Map<String, Integer> scrs = new LinkedHashMap<>();
        scrs.put("a", 0);
        scrs.put("b", 0);
        scrs.put("c", 0);
        scrs.put("d", 0);
        scrs.put("e", 0);
        for (int i = 0; i < args[0].length(); i++) {
            char let = args[0].charAt(i);  
            if (let == 'a') scrs.put("a", scrs.get("a") + 1);
            if (let == 'A') scrs.put("a", scrs.get("a") - 1);
            if (let == 'b') scrs.put("b", scrs.get("b") + 1);
            if (let == 'B') scrs.put("b", scrs.get("b") - 1);
            if (let == 'c') scrs.put("c", scrs.get("c") + 1);
            if (let == 'C') scrs.put("c", scrs.get("c") - 1);
            if (let == 'd') scrs.put("d", scrs.get("d") + 1);
            if (let == 'D') scrs.put("d", scrs.get("D") - 1);
            if (let == 'e') scrs.put("e", scrs.get("e") + 1);
            if (let == 'E') scrs.put("e", scrs.get("e") - 1);
        }
        List<Entry<String, Integer>> srtdscrs = new ArrayList<>(scrs.entrySet());
        srtdscrs.sort(Entry.comparingByValue(Collections.reverseOrder()));
        srtdscrs.forEach(e -> System.out.print(e.getKey()+": "+e.getValue()+" "));
    }
}

1

u/GreySkiesPinkShoes May 30 '18
import numpy as np

#%% main 
# r/dailyprogrammer #361
if __name__=='__main__': 
    scores = np.zeros(5)
    encoded_scores = input("Enter the encoded scores: ")
    
    for character in encoded_scores:
        if(character.islower()):
            scores[ord(character)-97]+=1
        else:
            scores[ord(character)-65]-=1
    
    print(scores)

1

u/[deleted] May 30 '18 edited May 30 '18

C++: Utilize std::map to avoid duplicates, then create a std::vector<pair<char,int>> using the range constructor...then realize the problem description specifies exactly 5 players, so the std::map step 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).

int main()
{
string in_str;
cin >> in_str;

map<char, int> scores;
for (const char& c : in_str)
    scores[tolower(c)] += (isupper(c)) ? -1 : 1;

vector<pair<char, int>> scores_sorted(
    scores.begin(),
    scores.end());

sort(scores_sorted.begin(), scores_sorted.end(),
    [](const pair<char, int>& p1, const pair<char, int>& p2) {
        return p1.second > p2.second;});

for (const pair<char, int>& p : scores_sorted)
    cout << "{" << p.first << ", " << p.second << "}" << endl;

return 0;
}

INPUT:

EbAAdbBEaBaaBBdAccbeebaec

OUTPUT:

{c, 3}
{d, 2}
{a, 1}
{e, 1}
{b, 0}

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.

scoreboard = {
	'a' : 0,
	'b' : 0,
	'c' : 0,
	'd' : 0,
	'e' : 0
}

def tally(text):
	for i in text:
		if i in scoreboard:
			scoreboard[i] += 1
		else:
			scoreboard[i.lower()] -= 1 

	print scoreboard

tally('EbAAdbBEaBaaBBdAccbeebaec')

1

u/billc128 May 30 '18 edited May 30 '18

Elixir solution

defmodule Series do
  def tally(key, acc) do
    cond do
      String.upcase(key) == key ->
        Map.update(acc, String.downcase(key), -1, fn value -> value - 1 end)
      true ->
        Map.update(acc, key, 1, fn value -> value + 1 end)
    end
  end
end

"EbAAdbBEaBaaBBdAccbeebaec"
|> String.codepoints
|> Enum.reduce(%{}, &Series.tally/2)
|> IO.inspect
# %{"a" => 1, "b" => 0, "c" => 3, "d" => 2, "e" => 1}

1

u/NorthwestWolf May 31 '18

Python 3

from collections import defaultdict

def tally(score_card):

    scores = defaultdict(int)
    
    for mark in score_card:
        if mark.islower():
            scores[mark.lower()] += 1
        elif mark.isupper:
            scores[mark.lower()] -= 1

    return scores

1

u/Digabble May 31 '18

C++: First post (technically second, had formatting issues). Feedback would be much appreciated.

include <iostream>
include <string>
include <cctype>
using namespace std;

int count(char&);

int main(int argc, char* argv[]){ 
string word = "EbAAdbBEaBaaBBdAccbeebaec";
int result[5];
char occurrence[5] = {'a','b','c','d','e'};

for(int i = 0; i < 5; i++){
    result[i]= 0;
}
for(unsigned int i = 0; i < word.length();++i){
    
    if (islower(word[i]))
        result[count(word[i])]++;
    
    if (isupper(word[i]))
        result[count(word[i])]--;
}
for(int i =0;i < 5; i++){
    cout << occurrence[i] << ": " << result[i] << endl;
    }

return 0;
}
int count(char& name){
name = tolower(name);
    switch(name){ 
        case 'a':
            return 0;
            break;
        case 'b':
            return 1;
            break;
        case 'c':
            return 2;
            break;
        case 'd':
            return 3;
            break;
        case 'e':
            return 4;
            break;
    }
return 0;
}

1

u/arachnidGrip Jun 01 '18

Haskell:

module DailyProgrammer.Prompt361.Easy (
    main
) where

import Data.Char (toUpper)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe

import System.Environment

main :: IO ()
main = do
    args <- getArgs
    let files = take 2 args
        inFile = if length files > 0 then head files else "DailyProgrammer/Prompt361/easySampleInput.txt"
        outFile = if length files > 1 then files !! 1 else "DailyProgrammer/Prompt361/easySampleOutput.txt"
    readFile inFile >>= writeFile outFile . unlines . map scoreGame . lines

scoreGame :: String -> String
scoreGame points = ret
  where
    -- Sum gained and lost points separately
    countPoints :: String -> Map Char Int
    countPoints "" = Map.empty
    countPoints (c:rest) = Map.insertWith (+) c 1 $ countPoints rest
    -- Sum gained and lost points for total scores
    sumUD :: [Char] -> Map Char Int -> Map Char Int
    sumUD [] m = m
    sumUD (key:keys) m = Map.insertWith (+) key (0 - (fromMaybe 0 (Map.lookup (toUpper key) m))) $ sumUD keys m
    showInt :: Int -> String
    showInt = show
    pointsM = sumUD "abcde" $ countPoints points -- build scorecard
    Just pointsA = Map.lookup 'a' pointsM -- get values on scorecard
    Just pointsB = Map.lookup 'b' pointsM -- get values on scorecard
    Just pointsC = Map.lookup 'c' pointsM -- get values on scorecard
    Just pointsD = Map.lookup 'd' pointsM -- get values on scorecard
    Just pointsE = Map.lookup 'e' pointsM -- get values on scorecard
    ret = "a:" ++ showInt pointsA ++ ", b:" ++ showInt pointsB ++ ", c:" ++ showInt pointsC ++ ", d:" ++ showInt pointsD ++ ", e:" ++ showInt pointsE

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.

inp = input("Enter the input player scores: ")

players = ['a', 'b', 'c', 'd', 'e']
 
result = {} 
for player in players:
  result[player] = inp.count(player) - inp.count(player.upper())

result_sorted = sorted(result.items(), key=lambda pair: pair[1], reverse=True)
print(result_sorted)

1

u/itachi_2017 Jun 01 '18 edited Jun 01 '18

C++

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

bool comp(pair<char,int> i1, pair<char,int> i2) {
    return i1.second>i2.second;
}

int main()
{
    ios_base::sync_with_stdio(false);
    string s;
    while (1) {
        cin >> s;
        vector<pair<char,int>> v {{'a',0}, {'b',0},{'c',0},{'d',0},{'e',0}};
        for (auto c:s) {
            if (c>='a' and c<='e') {
                v[c-'a'].second++;
            }
            else {
                v[c-'A'].second--;
            }
        }
        sort(v.begin(),v.end(),comp);
        cout << v[0].first << ":" << v[0].second;
        for (int i=1;i<v.size();i++) {
            cout<< ", " << v[i].first << ":" <<v[i].second;
        }
        cout << endl;
    }
    return 0;
}

Input

EbAAdbBEaBaaBBdAccbeebaec

Output

c:3, d:2, a:1, e:1, b:0

1

u/pbl24 Jun 01 '18

Python 2.7:

def sort(s):
    return sorted(s.iteritems(), key=lambda (k, v): v, reverse=True)

def format(s):
    return ', '.join('%s:%d' % (t[0], t[1]) for t in s)

def run(i):
    s = defaultdict(int)
    for c in i:
        s[c.lower()] += 1 if c.islower() else -1
    return format(sort(s))

print(run('abcde'))
print(run('dbbaCEDbdAacCEAadcB'))
print(run('EbAAdbBEaBaaBBdAccbeebaec'))

1

u/TheAnonymousCub Jun 02 '18 edited Jun 02 '18

C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace Tally
{
    public class Friend {
        public int id;
        public string name;
        public int score;
        public Friend(char id, string name = "", int score = 0) {
            this.id = id;
            this.name = name;
            this.score = score;
        }
    }
    public class Program
    {
        public static void Main(string[] args) 
        {
            Console.WriteLine("Welcome to tally!");
            Console.Write("Number of friends: ");
            int no = Convert.ToInt32(Console.ReadLine());
            IDictionary<char, Friend> friends = new Dictionary<char, Friend>();
            for (int i=0; i<no; i++) {
                Console.Write("Letter of friend " + (i + 1) + ": ");
                char[] letter = Console.ReadLine().ToCharArray();
                Console.Write("Name of friend " + (i + 1) + ": ");
                string name = Console.ReadLine();
                friends.Add(letter[0], new Friend(letter[0], name));
            }
            string input = "start";
            while (input != "") {
                Console.Write("Update Score: ");
                input = Console.ReadLine();
                char[] f = input.ToCharArray();
                for (int i=0; i<f.Length; i++) {
                    if (char.IsLower(f[i])) {
                        friends[char.ToLower(f[i])].score++;
                    } else {
                        friends[char.ToLower(f[i])].score--;
                    }
                }
            }
            var sorted = friends.OrderBy(x => x.Value.score);
            foreach (var e in sorted) {
                Console.WriteLine("Name: " + e.Value.name + ", Score: " + e.Value.score);
            }
        }
    }
}

Console:

Welcome to tally!
Number of friends: 5
Letter of friend 1: a
Name of friend 1: adam
Letter of friend 2: b
Name of friend 2: ben
Letter of friend 3: c
Name of friend 3: cody
Letter of friend 4: d
Name of friend 4: daniel
Letter of friend 5: e
Name of friend 5: ethan
Update Score: EbAAdbBEaBaaBBdAccbeebaec
Update Score:
Name: ben, Score: 0
Name: adam, Score: 1
Name: ethan, Score: 1
Name: daniel, Score: 2
Name: cody, Score: 3

1

u/[deleted] Jun 03 '18

My Java solution

 package easy;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

//https://www.reddit.com/r/dailyprogrammer/comments/8jcffg/20180514_challenge_361_easy_tally_program/

public class TallyProgram {

public static void main(String[] args) {
	String input = "EbAAdbBEaBaaBBdAccbeebaec";
	Map<String, Integer> scores = new HashMap<>();

	scores.put("a", 0);
	scores.put("b", 0);
	scores.put("c", 0);
	scores.put("d", 0);
	scores.put("e", 0);

	for (char c : input.toCharArray()) {
		if (Character.isUpperCase(c)) {
			int score = scores.get(String.valueOf(c).toLowerCase()) - 1;
			scores.put(String.valueOf(c).toLowerCase(), score);
		} else {
			int score = scores.get(String.valueOf(c).toLowerCase()) + 1;
			scores.put(String.valueOf(c).toLowerCase(), score);
		}
	}

	Map<String, Integer> result2 = new LinkedHashMap<>();
	scores.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
			.forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));

//		System.out.println("Unsorted:");
//		scores.forEach((k, v) -> System.out.println("Item : " + k + " Count : " + v));
	System.out.println("Sorted:");
	result2.forEach((k, v) -> System.out.println("Item : " + k + " Count : " + v));

}
}

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

dbbd

should d come first or it doesn't matter?

Python 3 (without sorting in incoming order though): from collections import defaultdict

def score_friends(score_string: str):
    scores = defaultdict(int)
    for score in score_string:
        score_id = score.lower()
        if score.isupper():
            scores[score_id] -= 1
        else:
            scores[score_id] += 1
    return sorted(scores.items(), key=lambda s: -s[1])

1

u/SuasoriaSatura Jun 03 '18

Python 3.6

input = "EbAAdbBEaBaaBBdAccbeebaec"
scores = {i.lower():0 for i in input}

for char in input:
	scores[char.lower()] += 1 if char.islower() else -1

print(sorted(scores.items(), key=lambda x: x[1], reverse=True))

1

u/MusicalCoder Jun 04 '18

Python 3.6.5:

def get_scores(results):
    scores = {}
    for c in results:
        if c.lower() in scores:
            scores[c.lower()] += 1 if c.islower() else -1
        else:
            scores[c.lower()] = 1 if c.islower() else -1
    return ', '.join(f"{k}:{scores[k]}" for k in sorted(scores, key=scores.get, reverse=True))


input = "EbAAdbBEaBaaBBdAccbeebaec"
print(get_scores(input))

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.

void Main()
{
	var scoreString = "dbbaCEDbdAacCEAadcBASD;LFKJQ;lkasjdfklqewrtl;kjc sadlkjw4e3asdf";
	var playerList = new List<Player> { };

	foreach (char c in scoreString)
	{
		if (!playerList.Any(p => p.Name.ToLower().Equals(c.ToString().ToLower())))
		{
			//create player only for ascii alphabet chars
			if ((65 <= c && c <= 90) || (97 <= c && c <= 122))
			{
				playerList.Add(new Player(c.ToString().ToLower()));
			}
			//move to next char otherwise
			else continue;
		}
		
		var player = playerList.FirstOrDefault(p => p.Name == c.ToString().ToLower());
		player.AddScore(c);
	}
	//sort from highest to lowest score
	playerList.Sort((p1, p2) => p2.Score.CompareTo(p1.Score));
	foreach (var player in playerList)
	{
		Console.WriteLine(player.ToString());
	}
}

private class Player
{
	public Player(string name, int score = 0)
	{
		Name = name;
	}
	private string _name;
	public string Name
	{
		get { return _name; }
		set { _name = value; }
	}
	private int score;
	public int Score
	{
		get { return score; }
		set { score = value; }
	}
	public override string ToString()
	{
		return Name + " : " + score.ToString();
	}
	
	public void AddScore( char c)
	{
		//ascii uppers
		if (65 <= c && c <= 90)
			score--;
		//ascii lowers
		if (97 <= c && c <= 122)
			score++;			
	}
}

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 input

For example: rdmd tally.d dbbaCEDbdAacCEAadcB

import 
    std.stdio, 
    std.ascii, 
    std.utf, 
    std.algorithm;

void main(string[] args)
{
	int[char] scores = ['a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0];
	string input = args[1];

	foreach (c; input)
	{
		if (c.isUpper)
			scores[c.toLower]--;
		else
			scores[c]++;
	}

	foreach (letter; scores.keys.byCodeUnit.sort)
		write(letter, ":", scores[letter], ", ");

	writeln();
}

1

u/pedro_fartinez Jun 05 '18

Python 3.6

Input:

import operator

def score_settle(string):
	players = {k.lower():0 for k in string}
	for x in string:
		if x.isupper():
                    players[x.lower()] -= 1
		else:
                    players[x] += 1
	return dict(sorted(players.items(), key=operator.itemgetter(1), reverse=True))

print(score_settle('EbAAdbBEaBaaBBdAccbeebaec'))

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?

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Map<Character, Integer> results = new HashMap<>();

        char[] input = "dbbaCEDbdAacCEAadcB".toCharArray();

        for (char c: input) {
            if (!results.containsKey(Character.toLowerCase(c))) {
                results.put(Character.toLowerCase(c), 0);
            }
            if (Character.isLowerCase(c)) {
                results.put(c, results.get(c) + 1);
            } else if (Character.isUpperCase(c)) {
                results.put(Character.toLowerCase(c), results.get(Character.toLowerCase(c)) - 1);
            }
        }

        List<Team> orderedResults = new ArrayList<>();

        for (Character c: results.keySet()) {
            orderedResults.add(new Team(c, results.get(c)));
        }

        Collections.sort(orderedResults, Collections.reverseOrder());

        for (Team t: orderedResults) {
            System.out.print(t);
        }
    }
}



public class Team implements Comparable<Team> {

    private char team;
    private int score;

    public Team(char team, int score) {
        this.team = team;
        this.score = score;
    }

    @Override
    public int compareTo(Team team) {
        if (this.score < team.score) {
            return -1;
        } else if (this.score > team.score) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public String toString() {
        return team + ":" + score + ", ";
    }
}

1

u/[deleted] Jun 05 '18

Python

Not sure if this has been tried yet, but what the heck.

Input:

def tally(score):
    list1=[]
    dict1 = dict()
    for i in score:
        list1.append(i)
    for r in list1:
        dict1[r.lower()] = dict1.get(r.lower(),0)+1
        if r==r.upper():
            dict1[r.lower()]=dict1[r.lower()]-2
    print(dict(sorted(dict1.items(), key=lambda x: x[1], reverse=True)))

tally('EbAAdbBEaBaaBBdAccbeebaec')
tally('abcde')
tally('dbbaCEDbdAacCEAadcB')

Output:

{'c': 3, 'd': 2, 'e': 1, 'a': 1, 'b': 0}
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1}
{'d': 2, 'b': 2, 'a': 1, 'c': 0, 'e': -2}

2

u/correct_misnomer Jun 07 '18

You don't need to convert the string to a list, for loops can iterate through strings the same way

for letter in string

or if you insist on having it as a list, just do

list(string)

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 :)

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
	public static void Main()
	{
		//Prompt for and receive input
		Console.WriteLine("Enter tally input");
		string inp = Console.ReadLine();
		
		//Split the input string into an array
		char[] arr = inp.ToCharArray();
		
		//Creating a dictionary to store value pairs for {individual, score}
		Dictionary<string, int> keyPairs = new Dictionary<string, int>();
		
		//Loop through array of individuals
		for(int i = 0; i < arr.Length; i++){
			
			//Instance vars for more visible code
			string indexStr = arr[i].ToString();
			string indexStrLow = indexStr.ToLower();
			bool isUpper = char.IsUpper(arr[i]);
			
			//Check and set values in dictionary to match gain and loss
			if (keyPairs.ContainsKey(indexStrLow)){
				if(isUpper){
					keyPairs[indexStrLow] = keyPairs[indexStrLow] - 1;
				} else 
					keyPairs[indexStr] = keyPairs[indexStr] + 1;
			} else{
				if(isUpper){
					keyPairs.Add(indexStrLow, -1);
				} else keyPairs.Add(indexStrLow, 1);
			}
		}
		
		//Sort dictionary
		var items = from pair in keyPairs orderby pair.Value descending select pair;
		
		//Display results
		foreach (KeyValuePair<string, int> pair in items){
			Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
		}
	}
	
}
    Outputs: c:3, d:2, e:1, a:1, b:0

Input: EbAAdbBEaBaaBBdAccbeebaec

1

u/Average-consumer Jun 05 '18

Haskell

import Data.Char

scores :: String -> [(Char, Int)]
scores s = map ((,) <*> ((-) . wins <*> losses)) . nub . map toLower $ s
  where
    wins c   = length $ filter (==c) s
    losses c = length $ filter (==(toUpper c)) s

Output

λ> mapM_ (print . scores) ["abcde", "dbbaCEDbdAacCEAadcB", "EbAAdbBEaBaaBBdAccbeebaec"]
[('a',1),('b',1),('c',1),('d',1),('e',1)]
[('d',2),('b',2),('a',1),('c',0),('e',-2)]
[('e',1),('b',0),('a',1),('d',2),('c',3)]

1

u/maggierose9194 Jun 06 '18
class Game
    def initialize
        @game = {}
    end

    def play(letters)
        letters.each do |letter|
            if @game[letter.downcase]
                participant = @game[letter.downcase]
            else
                participant = Participant.new(letter.downcase)
                @game[letter.downcase] = participant
            end
            participant.assign_points(letter)
        end
    end

    def participants
        @game.values
    end
end

class Participant
    attr_reader :score, :letter
    def initialize(letter)
        @letter = letter
        @score = 0
    end

    def assign_points(letter)
        if letter.match(/[a-z]/)
            @score += 1
        elsif letter.match(/[A-Z]/)
            @score -= 1
        else
            # "input is not valid"
        end
    end
end

puts "Input Users first initial:"
initial = gets
played_game = Game.new
played_game.play(initial.split(//))


played_game.participants.each do |participant|
    puts participant.letter + ": " + participant.score.to_s
end

1

u/__8021m1208__ Jun 06 '18

Python 3

s = input()
pl = list('abcde')
scorecard = list(map(lambda n : (s.count(n)-s.count(n.upper()),n),pl))
scorecard.sort()
for score,players in scorecard:
    print('%s = %s'%(players,score))

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:

.globl main

# Syscalls.
.eqv PRINT_INTEGER      1
.eqv READ_STRING        8
.eqv PRINT_CHARACTER    11
.eqv EXIT_SUCCESS       10

# Custom constants.
.eqv MAX_INPUT_STRING       1000

# Custom constants that are loaded to a global register ($s0, $s1, ...).
.eqv $CHAR_A                $s0
.eqv $CHAR_a                $s1

.eqv $PLAYERS_SCORE_ADDRESS $s2

.data
    # Player scores and names.
    PLAYERS: .word 0, 'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e'

    .align 2

    # Input string.
    INPUT_DATA: .byte 0:MAX_INPUT_STRING

.text

.macro print_single_character_macro (%character)
    li $v0, PRINT_CHARACTER
    li $a0, %character
    syscall
.end_macro

.macro print_number_macro (%tuple)
    li $v0, PRINT_CHARACTER
    lb $a0, 4(%tuple)
    syscall

    li $v0, PRINT_CHARACTER
    li $a0, ':'
    syscall

    li $v0, PRINT_INTEGER
    lw $a0, 0(%tuple)
    syscall
.end_macro

# $a0 -> character to process
.eqv $CHAR_PSC      $a0
.eqv $PLAYER_SCORES_PSC $t0
.eqv $SCORE_PSC     $t1
process_single_character:
    blt $CHAR_PSC, $CHAR_a, is_uppercase_psc

    # Point earned.
    # Get index of players scores array.
    sub $PLAYER_SCORES_PSC, $CHAR_PSC, $CHAR_a
    # Align logical index to real address index.
    sll $PLAYER_SCORES_PSC, $PLAYER_SCORES_PSC, 3
    # Get the address.
    add $PLAYER_SCORES_PSC, $PLAYERS_SCORE_ADDRESS, $PLAYER_SCORES_PSC

    lw $SCORE_PSC, ($PLAYER_SCORES_PSC)
    addi $SCORE_PSC, $SCORE_PSC, 1
    sw $SCORE_PSC, ($PLAYER_SCORES_PSC)

    j end_process_psc

    is_uppercase_psc:   # Point lost.

    sub $PLAYER_SCORES_PSC, $CHAR_PSC, $CHAR_A
    sll $PLAYER_SCORES_PSC, $PLAYER_SCORES_PSC, 3
    add $PLAYER_SCORES_PSC, $PLAYERS_SCORE_ADDRESS, $PLAYER_SCORES_PSC

    lw $SCORE_PSC, ($PLAYER_SCORES_PSC)
    addi $SCORE_PSC, $SCORE_PSC, -1
    sw $SCORE_PSC, ($PLAYER_SCORES_PSC)

    end_process_psc:

    jr $ra

# $a0 -> address of array to sort
# $a1 -> address of the last element of the array
.eqv $SWAPPED_BS        $t0
.eqv $CHAR_ADDRESS_BS       $t1
.eqv $FIRST_VALUE_BS        $t2
.eqv $SECOND_VALUE_BS       $t3
bubble_sort:
    li $SWAPPED_BS, 1   # Bubble sort's flag (true)

    # Last tuple of the array.
    addi $a1, $a1, -8

    while_bubble_sort:
        beq $SWAPPED_BS, $zero, stop_sort_bs
        li $SWAPPED_BS, 0

        move $CHAR_ADDRESS_BS, $a0
        for_bubble_sort_bs:
            beq $CHAR_ADDRESS_BS, $a1, stop_for_bs

            lw $FIRST_VALUE_BS, 0($CHAR_ADDRESS_BS)
            lw $SECOND_VALUE_BS, 8($CHAR_ADDRESS_BS)

            bge $FIRST_VALUE_BS, $SECOND_VALUE_BS, do_not_swap_bs
                li $SWAPPED_BS, 1

                # Swapping values.
                sw $FIRST_VALUE_BS, 8($CHAR_ADDRESS_BS)
                sw $SECOND_VALUE_BS, 0($CHAR_ADDRESS_BS)

                # Swapping players names.
                lw $FIRST_VALUE_BS, 4($CHAR_ADDRESS_BS)
                lw $SECOND_VALUE_BS, 12($CHAR_ADDRESS_BS)
                sb $FIRST_VALUE_BS, 12($CHAR_ADDRESS_BS)
                sb $SECOND_VALUE_BS, 4($CHAR_ADDRESS_BS)

            do_not_swap_bs:

            addi $CHAR_ADDRESS_BS, $CHAR_ADDRESS_BS, 8
            j for_bubble_sort_bs

        stop_for_bs:

        j while_bubble_sort

    stop_sort_bs:

    jr $ra

.eqv $CHAR_ADDRESS_M    $t9
.eqv $CHAR_M            $t8
main:
    # Load all global constants.
    li $CHAR_A, 'A'
    li $CHAR_a, 'a'
    la $PLAYERS_SCORE_ADDRESS, PLAYERS

    # Read the input string from the standard input.
    li $v0, READ_STRING
    la $a0, INPUT_DATA
    li $a1, MAX_INPUT_STRING
    syscall

    # Process every character.
    move $CHAR_ADDRESS_M, $a0

    while_process_main:
        lb $CHAR_M, 0($CHAR_ADDRESS_M)
        beq $CHAR_M, $zero, end_string_main

        move $a0, $CHAR_M
        jal process_single_character

        addi $CHAR_ADDRESS_M, $CHAR_ADDRESS_M, 1
        j while_process_main

    end_string_main:

    # Prints result.
    move $a0, $PLAYERS_SCORE_ADDRESS
    # Last element of players score (5 players * 8 byte).
    la $a1, 40($PLAYERS_SCORE_ADDRESS)
    jal bubble_sort 

    # Loop unrolled.
    move $CHAR_ADDRESS_M, $PLAYERS_SCORE_ADDRESS

    print_number_macro ($CHAR_ADDRESS_M)
    addi $CHAR_ADDRESS_M, $CHAR_ADDRESS_M, 8
    print_single_character_macro (',')
    print_single_character_macro (' ')

    print_number_macro ($CHAR_ADDRESS_M)
    addi $CHAR_ADDRESS_M, $CHAR_ADDRESS_M, 8
    print_single_character_macro (',')
    print_single_character_macro (' ')

    print_number_macro ($CHAR_ADDRESS_M)
    addi $CHAR_ADDRESS_M, $CHAR_ADDRESS_M, 8
    print_single_character_macro (',')
    print_single_character_macro (' ')

    print_number_macro ($CHAR_ADDRESS_M)
    addi $CHAR_ADDRESS_M, $CHAR_ADDRESS_M, 8
    print_single_character_macro (',')
    print_single_character_macro (' ')

    print_number_macro ($CHAR_ADDRESS_M)
    print_single_character_macro ('\n')


    # Exit program.
    li $v0, EXIT_SUCCESS
    syscall

Challenge input

user@thinkpad:~$ echo EbAAdbBEaBaaBBdAccbeebaec > test-input.in

Challenge output

user@thinkpad:~$ mars me nc sm program.asm < test-input.in

c:3, d:2, a:1, e:1, b:0

Bonus: how many instructions are executed

user@thinkpad:~$ mars me nc sm ic program.asm < test-input.in
c:3, d:2, a:1, e:1, b:0

639

2

u/CommonMisspellingBot Jun 06 '18

Hey, LelixSuper, just a quick heads-up:
enviroment is actually spelled environment. You can remember it by n before the m.
Have a nice day!

The parent commenter can reply with 'delete' to delete this comment.

→ More replies (1)

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

    ArrayList<String> strs = new ArrayList<String>();
    Scanner sc = new Scanner(System.in);
    String in = sc.nextLine(), print = "";
    char[] ab = {'a','b','c','d','e','A','B','C','D','E'};
    int[] values = new int[ab.length/2];
    for(int i=0;i<ab.length/2;i++){
        char c = ab[i];
        values[i] = (in.length() - in.replaceAll(Character.toString(ab[i]), "").length());
    }
    for(int i=ab.length/2;i<ab.length;i++)
        values[i-ab.length/2] -= (in.length() - in.replaceAll(Character.toString(ab[i]), "").length());
    int[] valuesSort = Arrays.copyOf(values, values.length);
    Arrays.sort(valuesSort);
    for(int i=valuesSort[valuesSort.length-1];i>=valuesSort[0];i--)
        for(int j=0;j<values.length;j++)
            if(values[j] == i) print = print + ab[j] + ": " + values[j] +"\n";
    System.out.println(print);

Output:

    b: 2
    d: 2
    a: 1
    c: 0
    e: -2

1

u/purdygilmour Jun 07 '18

PYTHON 3 Feedback Appreciated

import operator

input = 'EbAAdbBEaBaaBBdAccbeebaec'
scores = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}

for char in input:
    if char == char.upper():
        scores[char.lower()] -= 1
    elif char == char.lower():
        scores[char] += 1

scores = sorted(scores.items(), key=operator.itemgetter(1) , reverse=True)

print(scores)
→ More replies (1)

1

u/gabeace Jun 07 '18

C#

public class Player{
	public string Name {get; set;}
	public int Score {get; set;}

	public Player(string name)
	{
		Name = name;
		Score = 0;
	}
}

void TallyScores(string scores, bool sortByScore)
{
        List<Player> players = new List<Player>();
	string playerRep;

	for (int i = 0; i < scores.Length; i++) {
		playerRep = char.ToLower(scores[i]).ToString();

		if(!players.Exists(x => x.Name == playerRep))
			players.Add(new Player(playerRep));

		if(char.IsUpper(scores[i]))
			players.Find(x => x.Name == playerRep).Score--;
		else
			players.Find(x => x.Name == playerRep).Score++;
	}

	if(sortByScore)
		players = players.OrderByDescending(x => x.Score).ToList();
	else
		players = players.OrderBy(x => x.Name).ToList();

        Console.Writeline(GetScores(players));
}

string GetScores(List<Player> players)
{
	string result = "";
	int i = 0;

	foreach(Player player in players)
	{
		i++;
		result = string.Concat(result, player.Name, ": ", player.Score, i == players.Count ? "" : ", ");
	}			

	return result;
}

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

PLAYERS = 'abcde'


def get_player_scores(tallies):
    scores = [(player, tallies.count(player) - tallies.count(player.upper())) for player in PLAYERS]
    return sorted(scores, key=lambda x: x[1], reverse=True)


print(get_player_scores('abcde'))
print(get_player_scores('dbbaCEDbdAacCEAadcB'))
print(get_player_scores('EbAAdbBEaBaaBBdAccbeebaec'))    
→ More replies (1)

1

u/Vaakos_ Jun 09 '18

Python

First attempt to python. Have used some code from other users here to be honest.

#the list of friends
friends = {}

#challenge input
input="EbAAdbBEaBaaBBdAccbeebaec"

#for loop to check all values in input
for x in input:
    if x.lower() not in friends:
        friends[x.lower()] = 0
    if x.upper():
        friends[x.lower()] -= 1
    else:
        friends[x.lower()] += 1
        
#make a list from the dictionary
    friends_list = [(v, k) for k, v in friends.items()]
    
#sort the list and reverse
friends_list.sort()
friends_list.reverse()

#print the list
print(friends_list)

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:

import sys
import operator

def main():
    if len(sys.argv) > 1:
        players = set(sys.argv[1].lower())
        score_dict = {x:0 for i, x in enumerate(players)}
        score(sys.argv[1], score_dict)
        display(score_dict)

def score(string, score_dict):
    for c in string:
        if c in string and c.islower():
            score_dict[c] += 1
        if c in string and c.isupper():
            score_dict[c.lower()] -= 1

def display(score_dict):
    sorted_score = sorted(score_dict.items(), key=operator.itemgetter(1), reverse=True)
    print(", ".join("{}:{}".format(a[0], a[1]) for a in sorted_score))

if __name__ == "__main__":
    main()

1

u/xYuven Jun 10 '18

My try in C

char s[64] = "EbAAdbBEaBaaBBdAccbeebaec"

char players[6] = "abcde";
int score[5] = {0};

for (int i = 0; *(s+i) != '\0',i++)
    switch(*(s+i))
    {
        case 'a':
            score[0]++;
            break;
        case 'A':
            score[0]--;
            break;
        case 'b':
            score[1]++;
            break;
        case 'B':
            score[1]--;
            break;
        case 'c':
            score[2]++;
            break;
        case 'C':
            score[2]--;
            break;
        case 'd':
            score[3]++;
            break;
        case 'D':
            score[3]--;
            break;
        case 'e':
            score[4]++;
            break;
        case 'E':
            score[4]--;
            break;
    }

for(int i = 0;i<5;i++)
    printf("%c:%i",players[0],score[0]);

1

u/dustinroepsch Jun 11 '18

Python, needlessly complex

import itertools as i

game = 'EbAAdbBEaBaaBBdAccbeebaec'

print(sorted([(k, sum(map(lambda s: -1 if s.isupper() else 1, g))) for k, g in
              i.groupby(sorted(game, key=lambda s: s.lower()), key=lambda s: s.lower())],
             key=lambda p: p[1],
             reverse=True))

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.

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define USER_COUNT 5

typedef struct {
	char user;
	int num;
} Score;


int find(char ch, Score *scores, int size)
{
	for (int i = 0; i < size; i++) {
		if (scores[i].user == ch) {
			return i;
		}
	}
	return -1;
}

int compare(const void *a, const void *b) {
        Score *v1 = (Score *) a;
        Score *v2 = (Score *) b;
        return v2->num - v1->num;
}

void tally(char *inputs)
{
	Score scores[USER_COUNT];

        /* init score array */
	for (int i = 0; i < USER_COUNT; i++) {
		Score score = {.user = 'a' + i, .num = 0};
		scores[i] = score;
	}

        int length = strlen(inputs);
        int index;

	for (int i = 0; i < length; i++) {
		char ch = inputs[i];

                index = find(tolower(ch), scores, length);
		if (islower(ch)) {
			scores[index].num += 1;
		} else {
			scores[index].num -= 1;
		}
	}

        qsort(scores, USER_COUNT, sizeof(Score), compare);

	for (int i = 0; i < USER_COUNT; i++) {
		printf("%c:%d ", scores[i].user, scores[i].num);
	}
        printf("\n");
}

int main()
{
	tally("abcdef");
	tally("dbbaCEDbdAacCEAadcB");
        tally("EbAAdbBEaBaaBBdAccbeebaec");
        return 0;
}