Converting Celsius to Fahrenheit round-up

July 22, 2024
0 comments Go, Node, Python, Bun, Ruby, Rust, JavaScript

In the last couple of days, I've created variations of a simple algorithm to demonstrate how Celcius and Fahrenheit seem to relate to each other if you "mirror the number".
It wasn't supposed to be about the programming language. Still, I used Python in the first one and I noticed that since the code is simple, it could be fun to write variants of it in other languages.

  1. Converting Celsius to Fahrenheit with Python
  2. Converting Celsius to Fahrenheit with TypeScript
  3. Converting Celsius to Fahrenheit with Go
  4. Converting Celsius to Fahrenheit with Ruby
  5. Converting Celsius to Fahrenheit with Crystal
  6. Converting Celsius to Fahrenheit with Rust

It was a fun exercise.

Truncated! Read the rest by clicking the link below.

Converting Celsius to Fahrenheit with Crystal

July 19, 2024
0 comments Ruby

Previously in this series:

  1. Converting Celsius to Fahrenheit with Python
  2. TypeScript
  3. Go
  4. Ruby

Crystal?

Crystal is a general-purpose, object-oriented programming language. With syntax inspired by Ruby, it's a compiled language with static type-checking.


def c2f(c)
    c * 9.0 / 5 + 32;
end

def is_mirror(a, b)
    massage(a).reverse == massage(b)
end

def massage(n)
    if n < 10
        "0#{n}"
    elsif n >= 100
        massage(n - 100)
    else
        n.to_s
    end
end

def print_conv(c, f)
    puts "#{c}°C ~= #{f}°F"
end

(4...100).step(12).each do |c|
    f = c2f(c)
    if is_mirror(c, f.ceil.to_i)
        print_conv(c, f.ceil.to_i)
    elsif is_mirror(c, f.floor.to_i)
        print_conv(c, f.floor.to_i)
    else
        break
    end
end

And this is its diff with the Ruby version:


<     if is_mirror(c, f.ceil)
<         print_conv(c, f.ceil)
<     elsif is_mirror(c, f.floor)
<         print_conv(c, f.floor)
---
>     if is_mirror(c, f.ceil.to_i)
>         print_conv(c, f.ceil.to_i)
>     elsif is_mirror(c, f.floor.to_i)
>         print_conv(c, f.floor.to_i)

Run it like this:


crystal conversion.cr

or build and run:


crystal build -o conversion-cr conversion.cr
./conversion-cr

and the output becomes:

4°C ~= 40°F
16°C ~= 61°F
28°C ~= 82°F
40°C ~= 104°F
52°C ~= 125°F

Converting Celsius to Fahrenheit with Ruby

July 18, 2024
0 comments Ruby

This is a continuation of Converting Celsius to Fahrenheit with Python, and TypeScript, and Go but in Ruby:


def c2f(c)
    c * 9.0 / 5 + 32;
end

def is_mirror(a, b)
    def massage(n)
        if n < 10
            "0#{n}"
        elsif n >= 100
            massage(n - 100)
        else
            n.to_s
        end
    end
    massage(a).reverse == massage(b)
end

def print_conv(c, f)
    puts "#{c}°C ~= #{f}°F"
end

(4...100).step(12).each do |c|
    f = c2f(c)
    if is_mirror(c, f.ceil)
        print_conv(c, f.ceil)
    elsif is_mirror(c, f.floor)
        print_conv(c, f.floor)
    else
        break
    end
end

Run it like this:


ruby conversion.rb

and the output becomes:

4°C ~= 40°F
16°C ~= 61°F
28°C ~= 82°F
40°C ~= 104°F
52°C ~= 125°F
Previous page
Next page