29
\$\begingroup\$

In this challenge you and your friends are debating on which case is better, uppercase or lowercase? To find out, you write a program to do this for you.

Because esolangs scare your friends, and verbose code scares you, your code will need to be as short as possible.


Examples

PrOgRaMiNgPuZzLeS & CoDe GoLf
0.52 uppercase

DowNGoAT RiGHtGoAt LeFTGoat UpGoAT
0.58 uppercase

Foo BaR Baz
0.56 lowercase

Specifications

The input will consist only of ASCII characters. All non-alphabetic characters should be ignored. There will be at least 1 character of each case

The output should be the amount of the case that appears the most often over the total amount of alphabetic characters. It should be a decimal accurate to at least 2 decimal places. If uppercase appears more often, the output should end with uppercase, or lowercase.

There will never the the same amount of uppercase and lowercase characters.

\$\endgroup\$
7
  • 7
    \$\begingroup\$ Esolangs do not scare my friends. Does that mean my code can be wildly verbose? \$\endgroup\$
    – Alex A.
    Dec 25, 2015 at 17:21
  • \$\begingroup\$ @AlexA. verbose code scares you, so your code will also need to be golfed. \$\endgroup\$
    – Downgoat
    Dec 25, 2015 at 17:22
  • 17
    \$\begingroup\$ Oh right, I had forgotten about my recurring Java nightmares. \$\endgroup\$
    – Alex A.
    Dec 25, 2015 at 17:23
  • 4
    \$\begingroup\$ Will there be input with only one case? \$\endgroup\$
    – manatwork
    Dec 25, 2015 at 18:02
  • 1
    \$\begingroup\$ Does "accurate to at least 2 decimal places" require at least two decimals to be printed, or can a second decimal of zero be left out? \$\endgroup\$
    – hvd
    Dec 25, 2015 at 23:03

37 Answers 37

1
2
1
\$\begingroup\$

WDC 65816 machine code, 159 bytes

Hexdump:

00000000: a200 009b 8006 e600 9002 e601 b200 f016  ................
00000010: c941 90f2 c95b b003 e880 ebc9 6190 e7c9  .A...[......a...
00000020: 7bb0 e3c8 80e0 c220 8602 9865 0285 04c4  {...... ...e....
00000030: 0290 0ea9 6c6f 850f a977 6585 1184 0280  ....lo...we.....
00000040: 0aa9 7570 850f a970 6585 11a9 6400 8508  ..up...pe...d...
00000050: 6406 a900 0018 6502 c504 9006 e504 e606  d.....e.........
00000060: 80f6 c608 d0ef 0ac5 0490 02e6 06a9 302e  ..............0.
00000070: 850a a900 2085 0da9 7263 8513 a961 7385  .... ...rc...as.
00000080: 15a9 6500 8517 a230 00a5 06c9 0a00 9006  ..e....0........
00000090: e90a 00e8 80f5 e230 6930 850d 860c 60    .......0i0....`

Assembly:

; pointer to null-terminated string is in $00-$01
; enter in 8-bit accumulator mode (SEP #$20), 16-bit index mode (REP #$10)
; output is a null-terminated string starting at $0A, ends at $18
count:
    ; X - number of uppercase chars
    ; Y - number of lowercase chars
    LDX #$0000
    TXY
    BRA .firstiter
.loop:
    INC $00
    BCC .firstiter
    INC $01
.firstiter:
    LDA ($00)
    BEQ calc_maj
    CMP #$41 ; 'A'
    BCC .loop
    CMP #$5B ; 'Z'+1
    BCS +
    INX
    BRA .loop
+   CMP #$61 ; 'a'
    BCC .loop
    CMP #$7B ; 'z'+1
    BCS .loop
    INY
    BRA .loop
calc_maj:
    ; okay, now the hard part
    ; $02 is amount of lowercase
    ; $04 is total
    REP #$20
    STX $02
    TYA
    ADC $02
    STA $04
    CPY $02
    BCC +
    ; Y > X thus more lowercase
    LDA #$6F6C ; 'lo'
    STA $0A+5
    LDA #$6577 ; 'we'
    STA $0A+7
    STY $02
    BRA ++
+   ; Y < X => more uppercase
    LDA #$7075 ; 'up'
    STA $0A+5
    LDA #$6570 ; 'pe'
    STA $0A+7
++  ; now $02 is the amount of larger and the buffer's upper/lower word is already initialized
    LDA.w #100
    STA $08
    STZ $06
    LDA #$0000
.divmul_outer:
    CLC : ADC $02
.divmul_inner:
    CMP $04
    BCC +
    SBC $04
    INC $06
    BRA .divmul_inner
+   DEC $08
    BNE .divmul_outer
    ; if tmp * 2 >= $04: inc $06
    ASL
    CMP $04
    BCC +
    INC $06
+   ; $6 now contains $02 * #100 / $04
    ; let's write all the other letters while we're still in 16bit mode
    ; 0.XX XXXXrcase\0
    LDA #$2E30 ; "0."
    STA $0A
    LDA #$2000 ; "\0 "
    STA $0A+3
    LDA #$6372 ; "rc"
    STA $0A+9
    LDA #$7361 ; "as"
    STA $0A+11
    LDA #$0065 ; "e\0"
    STA $0A+13
    LDX #$0030
    LDA $06
-   CMP #$000A
    BCC +
    SBC #$000A
    INX
    BRA -
+   SEP #$30
    ADC #$30
    STA $0A+3
    STX $0A+2
    RTS

(assembled with Asar. The +/- things are labels.)

Input: A 16-bit pointer to the string to count, null terminated. Output: A null-terminated string starting at $0A (and ending at $18). Enter in 8-bit accumulator, 16-bit index mode. (SEP #$20 REP #$10)

God I hate this CPU. Will optimize tomorrow maybe.

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 124 121 119 118 bytes

u,l;f(char*x){for(u=l=0;*x;x++)isalpha(*x)?*x>96?l++:u++:0;printf("%f %sercase\n",fmax(l,u)/(u+l),u>l?"upp":"low");}

Try it online!

Slightly ungolfed

u,l;
f(char*x){
  for(u=l=0;*x;x++)
    isalpha(*x)?*x>96?l++:u++:0;
  printf("%f %sercase\n",fmax(l,u)/(u+l),u>l?"upp":"low");
}
\$\endgroup\$
0
\$\begingroup\$

Gema, 125 characters

\A=@set{l;0}@set{u;0}
<J1>=@incr{l}
<K1>=@incr{u}
?=
\Z=0.@div{@cmpn{$l;$u;$u;;$l}00;@add{$l;$u}} @cmpn{$l;$u;upp;;low}ercase

Sample run:

bash-4.3$ for input in 'PrOgRaMiNgPuZzLeS & CoDe GoLf' 'DowNGoAT RiGHtGoAt LeFTGoat UpGoAT' 'Foo BaR Baz'; do
>     gema '\A=@set{l;0}@set{u;0};<J1>=@incr{l};<K1>=@incr{u};?=;\Z=0.@div{@cmpn{$l;$u;$u;;$l}00;@add{$l;$u}} @cmpn{$l;$u;upp;;low}ercase' <<< "$input"
>     echo " <- $input"
> done
0.52 uppercase <- PrOgRaMiNgPuZzLeS & CoDe GoLf
0.58 uppercase <- DowNGoAT RiGHtGoAt LeFTGoat UpGoAT
0.55 lowercase <- Foo BaR Baz
\$\endgroup\$
1
  • \$\begingroup\$ -1 because esolangs scare your friends. (jk, upvoted) \$\endgroup\$
    – user46167
    Dec 25, 2015 at 21:43
0
\$\begingroup\$

Pure Bash, 183 185 182 179 bytes

+2 +4 bytes for more golfing.

(Second linebreak is there only to avoid scrollbar, so not counted).

o=({upp,low}ercase) q=;eval s+={A..Z};b="${1//[$s]}";eval s+={a..z}
c="${1//[$s]}";b=$[(${#1}-${#b})*64#fE/(${#1}-${#c})];((b<40#ck))&&
q=1 b=$[41#og-b];printf "%.2f ${o[q]}\n" .$b

Sample:

(set -- "Foo BaR Baz";o=({upp,low}ercase) q=;eval s+={A..Z};b="${1//[$s]}"
eval s+={a..z};c="${1//[$s]}";b=$[(${#1}-${#b})*64#fE/(${#1}-${#c})];((b<
40#ck))&&q=1 b=$[41#og-b];printf "%.2f ${o[q]}\n" .$b)
0.56 lowercase

or

ulRatio() { local b c s o=({upp,low}ercase) q=;eval s+={A..Z};b="${1//[$s]}"
eval s+={a..z};c="${1//[$s]}";b=$[(${#1}-${#b})*64#fE/(${#1}-${#c})];((b<40\
#ck))&&q=1 b=$[41#og-b];printf "%.2f ${o[q]}\n" .$b;}

for string in "PrOgRaMiNgPuZzLeS & CoDe GoLf" \
        "DowNGoAT RiGHtGoAt LeFTGoat UpGoAT" "Foo BaR Baz" ;do
    echo -e "\\n$string"
    ulRatio "$string"
  done

PrOgRaMiNgPuZzLeS & CoDe GoLf
0.52 uppercase

DowNGoAT RiGHtGoAt LeFTGoat UpGoAT
0.58 uppercase

Foo BaR Baz
0.56 lowercase
\$\endgroup\$
0
\$\begingroup\$

Matlab, 110 bytes

Solution

i=input('','s');a=sum(i>64&i<91);b=sum(i>96&i<123);d=a>b;disp([num2str(max(a,b)/(a+b)),' upp'*d+' low'*~d,'ercase'])

Test:

i='PrOgRaMiNgPuZzLeS & CoDe GoLf';
0.52 uppercase
i='DowNGoAT RiGHtGoAt LeFTGoat UpGoAT';
0.58065 uppercase
i='Foo BaR Baz'
0.55556 lowercase
\$\endgroup\$
0
\$\begingroup\$

Candy, 62 84 bytes

The legit answer is:

b0a(~x#97<{0|1}~y#32*XW-#65-=xX0#27w{ibY+})bZ/="ercase"A12/<{1A-p"upp"|Ap"low"}(;)N;

But adding a range operator and literal characters to Candy, it gets shorter:

(~`a`zw{i}A`A`Zw{v})"ercase"YZY+/~12/>{Ap"upp"|1A-p"low"}(;)N;

Long form as:

while
  peekA
  "a" "z" between
  if
    incrZ
  endif
  pushA
  "A" "Z" between
  if
    incrY
  endif
endwhile
"ercase"
pushY
pushZ pushY add
div
peekA
digit1 digit2 div
greater
if
  pushA print
  "upp"
else
  digit1 pushA sub print
  "low"
endif
while
  printChr
endwhile
digit10 printChr  # linefeed
\$\endgroup\$
0
\$\begingroup\$

Clojure, 149 bytes

#(let[C(for[c(re-seq #"[a-zA-Z]"%)](pos?(compare"["c)))[k v](last(sort-by val(frequencies C)))](str(float(/ v(count C)))" "(if k"upp""low")"ercase"))
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.