27
\$\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
    \$\begingroup\$ Esolangs do not scare my friends. Does that mean my code can be wildly verbose? \$\endgroup\$ – Alex A. Dec 25 '15 at 17:21
  • \$\begingroup\$ @AlexA. verbose code scares you, so your code will also need to be golfed. \$\endgroup\$ – Downgoat Dec 25 '15 at 17:22
  • 16
    \$\begingroup\$ Oh right, I had forgotten about my recurring Java nightmares. \$\endgroup\$ – Alex A. Dec 25 '15 at 17:23
  • 4
    \$\begingroup\$ Will there be input with only one case? \$\endgroup\$ – manatwork Dec 25 '15 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 '15 at 23:03

37 Answers 37

1
\$\begingroup\$

C (gcc), 124 121 119 bytes

u,l;f(char*x){for(u=l=0;*x;l+=*x>96&*x++<123)u+=*x>64&*x<91;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;l+=*x>96&*x++<123)
    u+=*x>64&*x<91;
  printf("%f %sercase\n",fmax(l,u)/(u+l),u>l?"upp":"low");
}
\$\endgroup\$
1
\$\begingroup\$

Javascript 80 characters

I'm bad at this but here was my go at Javascript:

a=(s)=>{let a=0,b=0;for(c in s)s[c]==s[c].toUpperCase()?a++:b++;return a/(a+b);}

Readable version:

a=(s)=>{
    let a=0,b=0;
    for(c in s)
        s[c]==s[c].toUpperCase()?a++:b++;
    return a/(a+b);
}

Function is called with: a('hEllO') which outputs 0.4 for example.

I feel like there must be a way to condense the arrow function or something with the words return or toUpperCase that I can do.

\$\endgroup\$
  • \$\begingroup\$ I think you also need to mention whether there was more uppercase or lowercase. And you need to print the one of which there was more (so if there's more uppercase than lowercase, you have to print that). \$\endgroup\$ – randomdude999 15 hours ago
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\$
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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