4
\$\begingroup\$

Task

Make a complete program or a function which calculates sum of all temperatures which, when written in Celsius and Fahrenheit scale, are anagram of each other.

Description

For example, 275 degree Celsius = 527 degree Fahrenheit, and 527 is anagram of 275. Non-integer temperatures (either in degree Celsius or degree Fahrenheit) and temperatures colder than 0 degree Celsius (like -40C = -40F) will not be considered.

Input

Input is a temperature in format ([1-9][0-9]*)(C|F) (example: 42F = 42 degree Fahrenheit, 125C = 125 degree Celsius, ...).

The program should be able to handle temperature less than 1000000C.

Output

Output is sum of all temperatures less than the temperature given by input, which, when written in Celsius and Fahrenheit scale, are anagram (without leading zeroes) of each other. Output will be two integers separated by a space. The first one will be the sum in degree Celsius, and the second one will be the sum in degree Fahrenheit.

If there's no such temperature, return or output 0 0.

Example

  • Input : 42C
    • There's no such temperature
    • Output : 0 0
  • Input : 300C
    • There's one temperature : 275C = 527F
    • Output : 275 527
  • Input : 10000F
    • There're three temperatures : 275C = 527F, 2345C = 4253F, and 4685C = 8465F
    • Output : 7305 13245
\$\endgroup\$
3
  • 6
    \$\begingroup\$ Hm, so 275 Coulomb are 527 Farad? ;-) \$\endgroup\$
    – Joey
    Commented Dec 23, 2011 at 18:39
  • \$\begingroup\$ I assume you want to exclude temperatures less than 0°C? \$\endgroup\$
    – dan04
    Commented Dec 24, 2011 at 17:24
  • \$\begingroup\$ @dan04 Yes. No temperatures less then 0 degree Celsius will be considered. \$\endgroup\$
    – JiminP
    Commented Dec 24, 2011 at 17:39

5 Answers 5

1
\$\begingroup\$

GolfScript, 65 61 characters

)\~{32- 9/5*}@70=*5/),0.@{.5*.`$@9*32+.`$@=:f*@+\f*@+\}%]' '*

Edit: incorporating the reduce operation into the loop saved 4 characters

\$\endgroup\$
2
  • \$\begingroup\$ C and F are different parities, so you can probably save a char by replacing 70= with 1& and working in the other scale. \$\endgroup\$ Commented Dec 29, 2011 at 14:19
  • \$\begingroup\$ @PeterTaylor Thank you for this hint. I also thought about that one but the other scale requires an offset of 32 which costs me 2 chars. Maybe I have to think about that again... \$\endgroup\$
    – Howard
    Commented Dec 29, 2011 at 14:49
1
\$\begingroup\$

Mathematica, 77

I wish I could get this shorter, but working with function names like IntegerDigits I think this is about the best I can do.

 Grid@{Total@Select[{#,9/5#+32}&~Array~#,SameQ@@(Sort@IntegerDigits@#&)/@#&]}&
\$\endgroup\$
1
\$\begingroup\$

C# - 166

Due to the small number of matches, I thought it might be easier to hardcode the outputs:

void F(string i){int n=int.Parse(i.Substring(0,i.Length-1));
n=i.Last()=='C'?n:5*(n-32)/9;
Console.Write(n>4685?"7305 13245":n>2345?"2620 4780":n>275?"275 527":"0 0");}

Edit: 352

To do it right requires a bit more work. Here's a first attempt at using a whole pile of linq:

using System;
using System.Linq;
class P{static void Main(string[] i)
{
   int n=int.Parse(i[0].Substring(0,i[0].Length-1));
   n=i[0].Last()=='C'?n:5*(n-32)/9;
   var q=Enumerable.Range(1,n).Where(x=>new string(((decimal)x*9/5+32).ToString().OrderBy(y=>y).ToArray())==new string(x.ToString().OrderBy(y=>y).ToArray()));
   Console.Write(q.Sum()+" "+q.Sum(x=>x*9/5+32));
}}

Maybe the 'use a pile of linq instead of a loop' approach hurt me too much. I'll try and shorten it.

Edit 2: 356

Since I'm already way behind in raw count, here's a one-liner from hell:

Console.Write(Enumerable.Range(1,i.Last()=='C'?int.Parse(i.Substring(0,i.Length-1)):5*(int.Parse(i.Substring(0,i.Length-1))-32)/9).Where(x=>new string(((decimal)x*9/5+32).ToString().OrderBy(y=>y).ToArray())==new string(x.ToString().OrderBy(y=>y).ToArray())).Aggregate("0 0",(a,t)=>(int.Parse(a.Split(' ')[0])+t)+" "+(int.Parse(a.Split(' ')[1])+t*9/5+32)))
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Your program does not give the correct result if the input exceeds 10985C. \$\endgroup\$
    – Howard
    Commented Dec 24, 2011 at 12:46
  • \$\begingroup\$ Dang. my mind must have skipped one of the zeroes in the problem statement. I could've sworn it said it only had to handle 10k and under. \$\endgroup\$
    – captncraig
    Commented Dec 25, 2011 at 15:24
  • \$\begingroup\$ You can save 12 chars by replacing i[0].Substring(0, i[0].Length - 1) with i[0].Split('C','F')[0] :) \$\endgroup\$
    – kev
    Commented Dec 28, 2011 at 8:36
0
\$\begingroup\$

Perl, 182 characters

sub F{5*($_[0]-32)/9}sub C{$_[0]*9/5+32}sub O{join'',sort split//,shift}<>=~/.$/;$c=$&;@s=(0,0);map{$v=&$c($_);if(O($_)==O($v)){$s[0]+=$_;$s[1]+=$v;}}0..$`;@s=sort{$a<=>$b}@s;say"@s"
\$\endgroup\$
0
\$\begingroup\$

MATL, 44 47 bytes

0&)Uqw70=?32- 5*9/]OOhw:"@t9*5/32+VY@Um?@4MhvXs

Try it online!

(+3 bytes to output 0 0 for no such temperatures (instead of empty output).)

\$\endgroup\$

Your Answer

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

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