Skip to content

Instantly share code, notes, and snippets.

@alexs77
Last active June 18, 2021 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexs77/b13900f2a24eddc23de130fafa4a38fc to your computer and use it in GitHub Desktop.
Save alexs77/b13900f2a24eddc23de130fafa4a38fc to your computer and use it in GitHub Desktop.
testing temperatur

Analyze temperatures

Find temperature closest to zero.

Read from stdin two lines:

  1. Number of temperatures to analyze
  2. A "number of temperatures". Integer values. Between -273 and +5526

Invocation

number_of_temps=50

~/tmp/Generate-Random-Numbers.sh "$number_of_temps" -273 5526 | python3 ~/tmp/temp-test.py
#!/bin/bash
count="$1"
min="$2"
max="$3"
echo "$count"
for ((i=1; i<=count; i++)); do
printf "%s " "$(($RANDOM%($max-$min+1)+$min))"
done
echo
import sys
import math
closest_temp = closest_no = 40_000
try:
n = int(input())
except EOFError:
print("0")
sys.exit(0)
if n == 0:
print("0")
sys.exit(0)
# Aktuell untersuchte Position der Temperatur
no = 0
for i in input().split():
t = int(i)
# Check if current temp is identical to
# closest temp found so far, as far as
# absolute values are concerned.
if abs(t) == abs(closest_temp):
# Yes, it's identical. If t is positive, store
# it. It is positive, if t > closests_temp
if t > closest_temp:
# Store temperature and number
closest_temp = t
closest_no = no
# Is current temperature closer than so far found closest_temp?
elif abs(t) < abs(closest_temp):
# Yes, it is. Store values.
closest_temp = t
closest_no = no
no += 1
# keine ahnung was die nächste Zeile sollte…
#print(0<<t)
print("Closest Temp:", closest_temp, "at position", closest_no, "with a distance from 0 of", abs(closest_temp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment