Three word location: Difference between revisions

added RPL
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
(added RPL)
 
(6 intermediate revisions by 6 users not shown)
Line 28:
Extra credit: Find a way to populate the word array with actual words.
 
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
-- Three-word location
-- J. Carter 2023 May
-- Uses the PragmAda Reusable Components (https://github.com/jrcarter/PragmARC)
 
with Ada.Text_IO;
with PragmARC.Images;
 
procedure Three_Word is
type U43 is mod 2 ** 43;
subtype Word is String (1 .. 6);
function Image is new PragmARC.Images.Modular_Image (Number => U43);
function Image is new PragmARC.Images.Float_Image (Number => Float);
Lat : constant String := "28.3852";
Long : constant String := "-81.5638";
LL_Mix : U43 := U43 ( (Float'Value (Lat) + 90.0) * 10_000.0) * 2 ** 22 + U43 ( (Float'Value (Long) + 180.0) * 10_000.0);
W3n : U43 := LL_Mix rem 2 ** 14; -- Number for 3rd word
W2n : U43 := (LL_Mix / 2 ** 14) rem 2 ** 14; -- " " 2nd "
W1n : U43 := LL_Mix / 2 ** 28; -- " " 1st "
W1 : constant Word := 'W' & Image (W1n, Width => 5, Zero_Filled => True); -- 1st word
W2 : constant Word := 'W' & Image (W2n, Width => 5, Zero_Filled => True); -- 2nd "
W3 : constant Word := 'W' & Image (W3n, Width => 5, Zero_Filled => True); -- 3rd "
begin -- Three_Word
Ada.Text_IO.Put (Item => Lat & ", " & Long & " => " & W1 & ' ' & W2 & ' ' & W3 & " => ");
-- Reverse the process
W1n := U43'Value (W1 (2 .. 6) );
W2n := U43'Value (W2 (2 .. 6) );
W3n := U43'Value (W3 (2 .. 6) );
LL_Mix := W1n * 2 ** 28 + W2n * 2 ** 14 + W3n;
Ada.Text_IO.Put_Line (Item => Image (Float (LL_Mix / 2 ** 22) / 10_000.0 - 90.0, Fore => 0, Aft => 4, Exp => 0) & ", " &
Image (Float (LL_Mix rem 2 ** 22) / 10_000.0 - 180.0, Fore => 0, Aft => 4, Exp => 0) );
end Three_Word;
</syntaxhighlight>
 
{{out}}
<pre>
28.3852, -81.5638 => W18497 W11324 W01322 => 28.3852, -81.5638
</pre>
 
=={{header|AppleScript}}==
===Index words===
When the words are index-based as in the task description, it's not necessary to generate all 28126.
<langsyntaxhighlight lang="applescript">on locationToWords({latitude, longitude})
-- "Convert" the coordinates to positive integers by adding enough degrees to ensure positive results,
-- multiplying by enough to left shift by four decimal places, and rounding.
Line 63 ⟶ 108:
set threeWords to locationToWords(location)
set checkLocation to wordsToLocation(threeWords)
return {location, threeWords, checkLocation}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{{28.3852, -81.5638}, {"W18497", "W11324", "W01322"}, {28.3852, -81.5638}}</langsyntaxhighlight>
 
===Actual words===
<langsyntaxhighlight lang="applescript">on locationToWords({latitude, longitude}, listOfWords)
script o
property wordList : listOfWords
Line 128 ⟶ 173:
set threeWords to locationToWords(location, o's wordList)
set checkLocation to wordsToLocation(threeWords, o's wordList)
return {location, threeWords, checkLocation}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{{28.3852, -81.5638}, {"quote", "hygiene", "aristotelean"}, {28.3852, -81.5638}}</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Conversion based on Wren<br>
WordList From link suggested by Symsyn
<langsyntaxhighlight AutoHotkeylang="autohotkey">URLDownloadToFile, http://www-personal.umich.edu/~jlawler/wordlist, % A_Temp "\wordlist.txt"
FileRead, wordList, % A_Temp "\wordlist.txt"
 
Line 186 ⟶ 231:
return [(ilat-900000)/10000, (ilon-1800000)/10000]
}
;-----------------------------------------------</langsyntaxhighlight>
{{out}}
<pre>LL = 28.3852, -81.5638
Line 195 ⟶ 240:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 253 ⟶ 298:
printf(" latitude = %0.4f, longitude = %0.4f\n", lat, lon);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 270 ⟶ 315:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Three_word_location;
 
Line 416 ⟶ 461:
end.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 433 ⟶ 478:
=={{header|FreeBASIC}}==
{{trans|Nim}}
<langsyntaxhighlight lang="freebasic">Print "Starting figures:"
Dim As Double lat = 28.3852, longi = -81.5638
Print Using " latitude = &, longitude = &"; lat; longi
Line 468 ⟶ 513:
Print !"\nAfter reversing the procedure:"
Print Using " latitude = &, longitude = &"; lat; longi
Sleep</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de Nim</pre>
Line 475 ⟶ 520:
{{trans|Wren}}
Though no need for big integers as we have int64 built in.
<langsyntaxhighlight lang="go">package main
 
import (
Line 530 ⟶ 575:
fmt.Println("\nAfter reversing the procedure:")
fmt.Printf(" latitude = %0.4f, longitude = %0.4f\n", lat, lon)
}</langsyntaxhighlight>
 
{{out}}
Line 539 ⟶ 584:
Three word location is:
W18497 W11324 W01322
 
After reversing the procedure:
latitude = 28.3852, longitude = -81.5638
</pre>
 
=={{header|J}}==
 
To take full advantage of the bit space, I think we should use 11650.8444 for the multiplier (2^21 - log2 180), but that's not what was asked for here.<syntaxhighlight lang="j">wloc=: {{ ;:inv wordlist{~ (15 14 14#i.3)#./.;(21 22#&.>2) #:&.> <.0.5+10000*90 180+y }}
colw=: {{ _90 _180+1e_4*21({.,&#.}.);(15 14 14#&.>2)#:&.>wordlist i.;:y }}</syntaxhighlight>
 
With <code>wordlist=: ('W',}.@":)&.> 1e5+i.3e4</code>
 
<syntaxhighlight lang="j"> wloc 28.3852 _81.5638
W18497 W11324 W01322
colw wloc 28.3852 _81.5638
28.3852 _81.5638</syntaxhighlight>
 
With <code>wordlist=: cutLF CR-.~fread 'wordlist'</code> based on the file 'wordlist' from http://www-personal.umich.edu/~jlawler/wordlist <syntaxhighlight lang="j"> wloc 28.3852 _81.5638
diplotene chamomile aeroplanist
colw wloc 28.3852 _81.5638
28.3852 _81.5638</syntaxhighlight>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
## Generic functions
 
# If $j is 0, then an error condition is raised;
# otherwise, assuming infinite-precision integer arithmetic,
# if the input and $j are integers, then the result will be an integer.
def idivide($j):
. as $i
| ($i % $j) as $mod
| ($i - $mod) / $j ;
 
# From bitwise.jq
# integer to stream of 0s and 1s, least significant bit first
def bitwise:
recurse( if . >= 2 then idivide(2) else empty end) | . % 2;
 
# inverse of `bitwise`
def stream_to_integer(stream):
reduce stream as $c ( {power:1 , ans: 0};
.ans += ($c * .power) | .power *= 2 )
| .ans;
 
# Convert the $j least-significant bits of the input integer to an integer
def to_int($j):
stream_to_integer(limit($j; bitwise));
 
# Take advantage of gojq's support for infinite-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
# Input is assumed to be a non-negative integer
def rightshift($n):
reduce range(0;$n) as $i (.; idivide(2)) ;
 
def lpad($len; $fill):
tostring
| ($len - length) as $l
| if $l <= 0 then .
else ($fill * $l)[:$l] + .
end;
 
## Functions to convert to and from the 'W00000' format
def toWord: "W\(lpad(5; "0"))";
 
def fromWord: .[1:] | tonumber;
 
# Latitude should be presented as a number in [-90, 90]
# and longitude as a number in [-180, 180].
def task($lat; $lon):
# convert lat and lon to positive integers
(($lat * 10000 | trunc) + 900000 ) as $ilat
| (($lon * 10000 | trunc) + 1800000) as $ilon
# build 43 bit integer comprising 21 bits (lat) and 22 bits (lon)
| ($ilat * (2 | power(22)) + $ilon) as $latlon
# isolate relevant bits
| ($latlon | rightshift(28) | to_int(15) | toWord) as $w1
| ($latlon | rightshift(14) | to_int(14) | toWord) as $w2
| ($latlon | to_int(14) | toWord) as $w3
| "Starting figures:",
" latitude = \($lat), longitude = \($lon)",
"\nThree word location is:",
([$w1, $w2, $w3] | join(" ")),
 
# now reverse the procedure
({}
| .latlon = ( ($w1 | fromWord) * (2 | power(28))
+ ($w2 | fromWord) * (2 | power(14))
+ ($w3 | fromWord ) )
| .ilat = (.latlon | rightshift(22))
| .ilon = (.latlon | to_int(22))
| .lat = ((.ilat - 900000) / 10000)
| .lon = ((.ilon - 1800000) / 10000)
| "\nAfter reversing the procedure:",
" latitude = \(.lat), longitude = \(.lon)" )
;
 
task(28.3852; -81.5638)
</syntaxhighlight>
{{output}}
<pre>
Starting figures:
latitude = 28.3852, longitude = -81.5638
 
Three word location is:
W18497 W11324 W01322
 
After reversing the procedure:
Line 546 ⟶ 704:
=={{header|Julia}}==
Direct translation from the SymSyn example given by the task creator, though note that idiomatic Julia would usually code this as two small encode() and decode() functions.
<langsyntaxhighlight lang="julia">
# Three Word Location - convert latitude and longitude to three words
LAT = 28.3852
Line 596 ⟶ 754:
# display values
println("latitude = $lat longitude = $lon")
</langsyntaxhighlight>{{out}}
<pre>
W18497 W11324 W01322
Line 603 ⟶ 761:
 
=== Idiomatic version with scrambling===
<langsyntaxhighlight lang="julia">using Random
 
const LAT = 28.3852
Line 644 ⟶ 802:
lat, lon = threeworddecode(words..., 12345678)
println("latitude = $lat longitude = $lon")
</langsyntaxhighlight>{{out}}
<pre>
W18497 W11324 W01322
Line 656 ⟶ 814:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">fun toWord(w: Long): String {
return "W%05d".format(w)
}
Line 707 ⟶ 865:
println("After reversing the procedure:")
println(" latitude = %.4f, longitude = %.4f".format(lat, lon))
}</langsyntaxhighlight>
{{out}}
<pre>Starting figures:
Line 720 ⟶ 878:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function toWord(w)
return string.format("W%05d", w)
end
Line 773 ⟶ 931:
-- and print the results
print("After reversing the procedure:")
print(string.format(" latitude = %0.4f, longitude = %0.4f", lat, lon))</langsyntaxhighlight>
{{out}}
<pre>Starting figures:
Line 786 ⟶ 944:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
 
func toWord(w: int64): string = &"W{w:05}"
Line 835 ⟶ 993:
# Print the results.
echo "\nAfter reversing the procedure:"
echo &" latitude = {lat:0.4f}, longitude = {long:0.4f}"</langsyntaxhighlight>
 
{{out}}
Line 849 ⟶ 1,007:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 906 ⟶ 1,064:
printf "Coordinates: %s, %s (%s)\n To Index: %s\n To 3-word: %s\nFrom 3-word: %s, %s\n From Index: %s, %s\n\n",
$lat, $lon, $description, join(' ',@index), join(' ',@words), w_decode(\@words), w_decode(\@index, sub { shift() });
}</langsyntaxhighlight>
{{out}}
<pre>Coordinates: 51.4337, -0.2141 (Wimbledon)
Line 946 ⟶ 1,104:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Three_word_location.exw
Line 1,005 ⟶ 1,163:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nAfter reversing the procedure:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" latitude = %0.4f, longitude = %0.4f\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">lat</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lon</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,039 ⟶ 1,197:
* u - long u (due boo moo)
 
<syntaxhighlight lang="raku" perl6line># SYNTHETICS HANDLING
my @synth = flat < b d f h j k l m n p r s t w y z > X~ < a e i o u >;
my %htnys = @synth.antipairs;
Line 1,074 ⟶ 1,232:
printf "Coordinates: %s, %s\n To Index: %s\n To 3-word: %s\nFrom 3-word: %s, %s\n From Index: %s, %s\n\n",
$lat, $lon, @index.Str, @words.Str, w-decode(@words), w-decode @index, :f( { $_ } );
}</langsyntaxhighlight>
{{out}}
 
Line 1,141 ⟶ 1,299:
10 times better accuracy in the same three, 6-letter word space.
 
<syntaxhighlight lang="raku" perl6line># SYNTHETICS HANDLING
my @synth = flat < b d f j k n p r s t w > X~ < a e i o u >;
my %htnys = @synth.antipairs;
Line 1,176 ⟶ 1,334:
printf "Coordinates: %s, %s\n To Index: %s\n To 3-word: %s\nFrom 3-word: %s, %s\n\n",
$lat, $lon, w-encode($lat, $lon, :f({$_})).Str, @words.Str, w-decode(@words);
}</langsyntaxhighlight>
{{out}}
<pre>Coordinates: 51.43372, -0.21412
Line 1,207 ⟶ 1,365:
To 3-word: ba duji be
From 3-word: -89.99999, -179.99999</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP-49C}}
« 43 STWS DEC
{ 90 180 } ADD 10000 * IP R→I
EVAL SWAP 4194304 * + R→B
1 2 '''START'''
#3FFFh AND LASTARG 1 + /
'''NEXT'''
3 →LIST REVLIST
100000 ADD
1 « →STR 4 OVER SIZE 1 - SUB "W" SWAP + » DOLIST
» '<span style="color:blue">LL→W</span>' STO <span style="color:grey">''@ ( { latitude longitude } → { "word1" .. "word3" )''</span>
« « 2 OVER SIZE SUB STR→ » MAP
DUP 1 GET
2 3 '''FOR''' j
16384 * OVER j GET +
'''NEXT'''
NIP R→B #400000h / LASTARG 1 - AND
2 →LIST B→R 10000 / { 90 180 } -
» '<span style="color:blue">W→LL</span>' STO <span style="color:grey">''@ ( → { "word1" .. "word3" → { latitude longitude } )''</span>
 
{ 28.3852 -81.5638 } <span style="color:blue">LL→W</span>
DUP <span style="color:blue">W→LL</span>
{{out}}
<pre>
2: { "W18497" "W11324" "W01322" }
1: { 28.3852 -81.5638 }
</pre>
 
=={{header|Symsyn}}==
<syntaxhighlight lang="symsyn">
<lang Symsyn>
| Three Word Location - convert latitude and longitude to three words
 
Line 1,320 ⟶ 1,508:
return -1
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,329 ⟶ 1,517:
Using Real Words
 
<syntaxhighlight lang="symsyn">
<lang Symsyn>
 
| Three Word Location - convert latitude and longitude to three words
Line 1,453 ⟶ 1,641:
return -1
 
</syntaxhighlight>
</lang>
 
{{Output}}
Line 1,461 ⟶ 1,649:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<langsyntaxhighlight lang="go">import strconv
fn to_word(w i64) string { return 'W${w:05}' }
Line 1,513 ⟶ 1,701:
println("\nAfter reversing the procedure:")
println(" latitude = ${lat:.4}, longitude = ${lon:.4}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,533 ⟶ 1,721:
 
Note that bitwise operations are limited to 32-bit unsigned integers in Wren which isn't big enough here so we use BigInts instead.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./big" for BigInt
 
// functions to convert to and from the word format 'W00000'
Line 1,579 ⟶ 1,767:
// and print the results
System.print("\nAfter reversing the procedure:")
Fmt.print(" latitude = $0.4f, longitude = $0.4f", lat, lon)</langsyntaxhighlight>
 
{{out}}
1,150

edits