analitics

Pages

Saturday, February 27, 2021

Python 2.7.18 : Kick Start Google - Boring Numbers.

Today I solved one of the problems required for solving Google Kick Start in 2020, see on my account.
Round H 2020 - Kick Start 2020 Boring Numbers (7pts, 12pts) Problem Ron read a book about boring numbers. According to the book, a positive number is called boring if all of the digits at even positions in the number are even and all of the digits at odd positions are odd. The digits are enumerated from left to right starting from 1. For example, the number 1478 is boring as the odd positions include the digits {1, 7} which are odd and even positions include the digits {4, 8} which are even. Given two numbers L and R, Ron wants to count how many numbers in the range [L, R] (L and R inclusive) are boring. Ron is unable to solve the problem, hence he needs your help.
Let's try to find the solution:
import math
print("Round H 2020 - Kick Start 2020: Detect is positive number is called boring.\n")
nr = int(input("get natural positive number: "))

all_cond = []
# detect if the number is odd or even number
def detect_odd_even(num):
  if (num % 2) == 0:  
    #print("{0} is Even number".format(num)) 
    detect = True  
  else:  
    #print("{0} is Odd number".format(num))  
    detect = False
  return detect
# check if is an boring number.
def boring_number(num):
    for p,num in enumerate(str(num),1):
        print(p,num)
        if (detect_odd_even(p) == detect_odd_even(int(num))): all_cond.append(True)
        else:
          all_cond.append(False)

# check the number is 
boring_number(nr)
# print result if the positive number is boring
if (all(all_cond) == True): print("{0} is an positive boring number".format(nr))
else:
  print("{0} is not a positive boring number".format(nr))
Let's test it:
~/mathissues$ python math_003.py 
Round H 2020 - Kick Start 2020: Detect is positive number is called boring.

get natural positive number: 345
(1, '3')
(2, '4')
(3, '5')
345 is an positive boring number
Then la last step is to test any number from range of given two numbers L and R.
The new source code is this:
import math
print("Round H 2020 - Kick Start 2020: Detect is positive number is called boring.\n")
#nr = int(input("get natural positive number: "))

all_cond = []
# detect if the number is odd or even number
def detect_odd_even(num):
  if (num % 2) == 0:  
    #print("{0} is Even number".format(num)) 
    detect = True  
  else:  
    #print("{0} is Odd number".format(num))  
    detect = False
  return detect
# check if is an boring number.
def boring_number(num):
    for p,num in enumerate(str(num),1):
        print(p,num)
        if (detect_odd_even(p) == detect_odd_even(int(num))): all_cond.append(True)
        else:
          all_cond.append(False)

# check the number is 
#boring_number(nr)
# print result if the positive number is boring

nr1 = int(input("get firt natural positive number: "))
nr2 = int(input("get firt natural positive number: "))
if nr1 < nr2:
  n1 = nr1
  n2 = nr2
else: 
  n1 = nr2
  n2 = nr1
for all_nr in range(n1,n2):
  all_cond = []
  boring_number(all_nr)
  print(all_nr)
# print result if the positive number is boring
  if (all(all_cond) == True): print("{0} is an positive boring number".format(all_nr))
  else: print("{0} is not a positive boring number".format(all_nr))
  all_cond = [] 
The final solution result to test all numbers in range given two numbers L and R can be see bellow:
~/mathissues$ python math_003.py 
Round H 2020 - Kick Start 2020: Detect is positive number is called boring.

get firt natural positive number: 11
get firt natural positive number: 16
(1, '1')
(2, '1')
11
11 is not a positive boring number
(1, '1')
(2, '2')
12
12 is an positive boring number
(1, '1')
(2, '3')
13
13 is not a positive boring number
(1, '1')
(2, '4')
14
14 is an positive boring number
(1, '1')
(2, '5')
15
15 is not a positive boring number

Thursday, February 25, 2021

Python 3.9.1 : Python and Spectator Earth.

The Spectator Earth allows you to track satellites, access their imagery, and easily build new information, based on data from this amazing orbital infrastructure.
You need just an API KEY to use it with cURL, Python, or JavaScript. 
In this tutorial, I will show you how can be used.
Login on the app.spectator.earth and get your API Key. 
Let's see some simple examples.
This first example shows you the satellite by id:
import requests

id = input("Type id sattelite: ")
satellite_id = id
url = 'https://api.spectator.earth/satellite/{satellite_id}'.format(satellite_id=satellite_id)

response = requests.get(url)
data = response.json()
print(data)
Let's see some satellite by ids:
[mythcat@desk Spectator]$ python  satellite_all.py 
Type id sattelite: 1
{'id': 1, 'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-51.88851761326367, 
70.75978200389422]}, 'properties': {'name': 'Sentinel-1A', 'norad_id': 39634, 'sensors': 
[{'type': 'SAR', 'avg_footprint_width': '3.20'}], 'open': True, 'platform': 'Sentinel-1A'}}
[mythcat@desk Spectator]$ python  satellite_all.py 
Type id sattelite: 2
{'id': 2, 'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [42.046601163641796, 
-14.660442921557335]}, 'properties': {'name': 'Sentinel-2A', 'norad_id': 40697, 'sensors': 
[{'type': 'OPTICAL', 'avg_footprint_width': '2.32'}], 'open': True, 'platform': 'Sentinel-2A'}}
[mythcat@desk Spectator]$ python  satellite_all.py 
Type id sattelite: 3
{'id': 3, 'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [-91.98478583784988, 
79.45732380441011]}, 'properties': {'name': 'Sentinel-3A', 'norad_id': 41335, 'sensors': 
[{'type': 'OPTICAL', 'avg_footprint_width': '10.16'}, {'type': 'OPTICAL', 
'avg_footprint_width': '11.20'}], 'open': True, 'platform': 'Sentinel-3A'}}
[mythcat@desk Spectator]$ python  satellite_all.py 
Type id sattelite: 4
Let's see one example with satellite overpasses:
import requests
import json
api_key = input("api_key :")
bbox = '19.59,49.90,20.33,50.21'
satellites = 'Sentinel-2A,Sentinel-2B'
url = 'https://api.spectator.earth/overpass/?api_key={api_key}&bbox={bbox}&
satellites={satellites}'.format(api_key=api_key, bbox=bbox, satellites=satellites)
response = requests.get(url)
data = response.json()
print(type(data))
print(data)
You can query for overpasses of the chosen satellite over a defined area of interest and the output provides you with a satellite sensor footprint for the nearest overpass, overpass frequency in seconds, and all information available.
Query parameters for the URL variable are:
The result is this:
[mythcat@desk Spectator]$ python satellite.py 
api_key :....
...
{'frequency': 93996, 'overpasses': [{'id': 16486437, 'acquisition': True, 'date': 
'2021-02-26T09:57:00Z', 'footprint': {'type': 'Polygon', 'coordinates': 
...
footprint': {'type': 'Polygon', 'coordinates': 
...
[[[19.153675312697636, 46.847554078717884], ... 
Query parameters for the url :
  • api_key - your API key
  • bbox - area of interest bounding box list of coords (lon, lat, lon, lat);
  • satellites - which satellites to include like: (Sentinel-1A, Sentinel-1A, Sentinel-2A, Sentinel-2B, Sentinel-3A, Landsat-8) - optional;
  • days_before - a number of days before the current date for which overpasses should be computed integer optional, default=0;
  • days_after - a number of days after the current date for which overpasses should be computed integer optional, default=7;
You can see a image get from my location: