Showing posts with label PRINT. Show all posts
Showing posts with label PRINT. Show all posts

Thursday, November 2, 2017

Adventures in Python: Using a Dictionary to Print a Table (OT: HP Prime new keyboard colors)

Adventures in Python:  Using a Dictionary to Print a Table
(OT: HP Prime new keyboard colors)

Here is a short program where a dictionary and the format command are used to print a nice looking table.

In Python, a dictionary is defined as list of two-element entries, in the format { x1:y1, x2:y2, x3:y3, … }.  Where x1, y1, x2, y2, and so on are strings or numbers. 

We can also designate the format of data within a print statement.  The general syntax:

print( a string that contains {n:ABC}.format(n0, n1, n2, …))

You can as many print formats as you want.  The format inside of {n:ABC}:
n = the nth argument, starting with 0
A = 0 for padded zeros, < align left, > align right, ^ align center
B = length of a field in the form of L.N  (L = length of the field, N = number of decimal places, note that L is the minimum filed length, not maximum)
C = d or i = integer, f - floating, r or s = string

Example: 
>>> import math

Print π with 10 decimal places:
>>> print('{0:1.10f}'.format(math.pi))
3.1415926536

Print π with 8 decimal places:
>>> print('{0:1.8f}'.format(math.pi))
3.14159265

Print the first five letters of the alphabet from a string of the entire alphabet:
>>> print('{0:10.5s}'.format('abcdefghijklmnopqrstuvwxyz'))
abcde

Sample script:

# Program 011:  Using format in print

# General syntax:

# print(' ... {n:ABC} '.format(x0, x1))
# n = the nth argument, starting with 0
# A = 0 for padded zeros, < align left, > align right, ^ align center
# B = length of a field
# C = d or i = integer, f - floating, r or s = string

# x0, x1 are items

# If a table is used, use a for loop with tablename.items
# Table sytnax =  { item0 : item1, [start a new row]}
# for x0, x1 in table.items():
#  print('...   '.format(x0,x1))
# For this, table can only have 2 times per row?


# table of the famous stars
table = {'Antares':'Scorpius', 'Regulus':'Leo',
         'Aldebaran':'Taurus', 'Sadalmelik':'Aquarius',
         'Siruis':'Canis Major', 'Vega':'Lyra',
         'Polaris':'Ursa Minor', 'Deneb':'Cygnus',
         'Alpha Centauri':'Centaurus', 'Altair':'Aquila',
         'Castor':'Gemini', 'Betelgeuse':'Orion',
         'Fomalhaut':'Piscis Austrinus', 'Spica':'Virgo'}

print('Astronomy\'s Famous Stars')
# need \' for the apostrophe
# for our example let x0 = star, x1 = constellation
for star, constellation in table.items():
    # 0 = star, 1 = constellation
    # <15s = left aligned, 15 spaces, string
    print('Star:  {0:<15s} ==> Constellation: {1:<15s}'
          .format(star,constellation))

Result:

Astronomy's Famous Stars
Star:  Antares         ==> Constellation: Scorpius      
Star:  Regulus         ==> Constellation: Leo           
Star:  Aldebaran       ==> Constellation: Taurus        
Star:  Sadalmelik      ==> Constellation: Aquarius      
Star:  Siruis          ==> Constellation: Canis Major   
Star:  Vega            ==> Constellation: Lyra          
Star:  Polaris         ==> Constellation: Ursa Minor    
Star:  Deneb           ==> Constellation: Cygnus        
Star:  Alpha Centauri  ==> Constellation: Centaurus     
Star:  Altair          ==> Constellation: Aquila        
Star:  Castor          ==> Constellation: Gemini        
Star:  Betelgeuse      ==> Constellation: Orion         
Star:  Fomalhaut       ==> Constellation: Piscis Austrinus
Star:  Spica           ==> Constellation: Virgo

Overtime

I finally got a new HP Prime with the new color contrast (darker blue shift text, slightly darker orange shift text, keys are a lighter color, the number keys are white), and yes, I do like the new design. (model number G8X92AA, hardware version C, 2016 edition)




Eddie


This blog is property of Edward Shore, 2017.

Tuesday, October 3, 2017

Adventures in Python: Printing Mathematical Symbols with Unicodes

Adventures in Python:  Printing Mathematical Symbols with Unicodes

Got to start somewhere.  I confess that I am a beginner when it comes to Python. 

With the use of the backslash, followed by a u, then four hexadecimal numbers, Python can print all sorts of symbols not easily found on a standard keyboard.  Some common math symbols and their Unicode:

039A
Δ
03C0
π
2202
2248
03A3
Σ
00B0
°
221A
2260
03A6
ϕ
0283
2264
221E
03C3
μ
03B4
δ
2265
03B8
θ
03BB
λ
2220
03B1
α
03B2
β
03C3
σ
0413
Γ
2205
29A8
2282
2283
221B
221D

A short program that demonstrates calling the Unicode characters:

# Program 001: Python Program
# Unicode is the format \uxxxx
# print command is used
print("This is some of my favorite constants (to 8 places).")
print("\u221A \u2248 1.41421356")
print("\u03C0 \u2248 3.14159265")
print("e \u2248 2.71828183")
print("\u03A6 \u2248 1.61803399")

Anything that comes after the hashtag (#) is a comment. 

Output:

This is some of my favorite constants (to 8 places).
√ ≈ 1.41421356
π ≈ 3.14159265
e ≈ 2.71828183
Φ ≈ 1.61803399
>>> 

Fairly simple.  Next up, I learn about input and the type of objects (the hard way).

Eddie


This blog is property of Edward Shore, 2017.

Sunday, July 2, 2017

App Review: Programmable Calculator (Jeff Glenn)

App Review:  Programmable Calculator (Jeff Glenn)

Programmable Calculator app screen shots

Author:  Jeff Glenn
Date: 2014
Cost:  99 cents
Version Reviewed: 1.2.0, 3/20/2015
Type: Programmable, Basic
Platform:  Android

Mathematical Programming From Scratch

The Programmable Calculator app lives up to its name.  It gives you barebones as far as features are concerned: just the four arithmetic functions (addition, subtraction, multiplication, and division) and nothing else.  Not even square root or π. 

Their website, http://igram.org/progcalc/user_manual.html, has a program to calculate square roots.

Before there were scientific calculators and scientific functions arrived on our computers, they had to all be programmed using just arithmetic functions. 

If you want to try scientific programming on this app, you may want to consult the 1975 book Scientific Analysis on the Pocket Calculator by Jon M. Smith (John Wiley & Sons), or this website by Ted Muller:  http://tedmuller.us/Math/Calculator-1'Introduction.htm 

Needless to the say, the Programmable Calculator operates on Chain logic.  That is, it doesn’t follow the algebraic order of operations ( 2, [ + ], 3,  [ * ], 4, [ = ] returns 20 instead of 14).

Press [ f ], [R/S] to access the options and user manual. Options include turning sound and vibration on for key presses, which I highly recommend. 

Programming

The programming features on this app are laid directly on the keyboard.  There are 10 memory registers (R0 through R9), 10 labels (again, 0 – 9), and the accumulator (number on the display) to use.  Programs can be saved and loaded with file names.  The limit seems to be the amount of memory on your Android device (phone).

What is weird is that certain commands, such as the comparison, store, and recall, will prompt you for either the accumulator (display, by pressing the [ . ]), a specific memory register (0 – 9), or a constant, which will require a press of the equals key [ = ] first.  This takes getting used to.

Here are some commands. 

[ IN ].  Input.  (in).   Stores the prompted value to a designated memory register.  This command also acts as a prompt.  If a print string proceeds the input command, the prompt gets attached to the end of the screen.

[ OUT ].  Output.  (out)   Displays the contents of a designated memory register.  Execution doesn’t stop on output, so if this command is the last command before eof (end of file), you’ll need either an R/S (run/stop) or sleep.  If a print string proceeds the output command, the value gets attached to the end of the print string.

[ PRT ].   Print  (prt).  Prints a string.  Without a string, this command would clear the display instead.

[ LBL] and [ GTO ].   Label (lbl) and go to (gto), respectively.

[ DSZ ].  Decrement and skip.  (dsz)  Designates a register to decrease by 1.  If the result is zero, the next command is stepped.

[ < ], [ = ], [ > ], ≤, ≠, ≥.  Comparisons.  Compares the accumulator (display) to a designated register.  If the test is true, the next command is executed.  Otherwise, the next command is skipped. Code names: lt, eq, gt, le, ne, and ge respectively.

[ SET ].  Sets the accumulator (display) to a designated value.

[ CLR ].  Clears the accumulator (display). 

[ STO ] and [ RCL ].  Store and recall.

The arithmetic keys: [ + ], [ - ], [ * ], [ ÷ ].  These keys work quite differently in program mode.  You are prompted to designate a register for the operation to work on (like recall arithmetic).  You can also choose the accumulator by pressing [ . ].  Pressing the equals key [ = ] allows for a numeric constant to be entered.  All numeric constants are shown as floating point numbers.  For example, add 1.0 would add 1, but add 1 would add the value of register 1.  Code names are add, sub, mul, and div, respectively.

[SLEEP]  This command pauses execution.  A value of 1,000 amounts to 1 second. 2,000 for 2 seconds, etc. Code name: slp.  This works similarly to the comparison and arithmetic commands.

[R/S]  Run/Stop.  Code name: stp.

[REM]  Remark, comment.

If I understand [TIME] correctly, this records the time value onto the calculator app.  I haven’t tried this yet.


Sample Programs

I suggest trying the sample programs listed below or listed online manual ( http://igram.org/progcalc/user_manual.html  ) before programing on your own.  I really got stuck with the input instruction and how to get the R/S key to work out.

Square Plus 1

This program calculates the function f(x) = x^2 + 1

Step
Line
Comment
00
prt “Number: “
Prompt
01
in 0
Input to R0
02
prt  (blank string)
Keys: [PRT], [DONE]
Clears the display
03
rcl 0

04
mul 0
R0 * R0
05
add 1.0
Keys: [ + ], [ = ], 1, [ = ]
06
sto 1
Store result in R1
07
out 1
Display R1
08
sleep 1000.0
Keys:  [ f ], [OUT] (SLEEP), [ = ], 1000, [ = ]
09
eof
End of Program

Example:
Input: 6, Result:  37
Input: 11, Result: 122

Area of a Circle

The value of π must be manually entered.  I use 10 digits.

Step
Line
Comment
00
rem “Area: Circle”
Remark
01
prt ([DONE])
Clear the display
02
prt “Radius: “
Prompt: “Radius”
03
in 0
Input to R0
04
prt

05
rcl 0

06
mul 0
R0 * R0
07
mul (=) 3.1415926535
Use [ = ] to enter the constant π
08
sto 1
Store result in R1
09
out 1
Display R1
10
eof
End of Program

Example:
Input:  2.5, Result:  19.634954084375

Power:  x^y

Conditions:  x > 0, y is an integer.  Neither condition is tested.  Since only arithmetic functions are available, a loop is used.

Step
Line
Comment
00
prt
Clear the display
01
prt “x = “
Prompt for x
02
in 0
Input R0
03
rcl 0

04
sto 2
Store R0 in R2 (copy x)
05
prt

06
prt “y (integer) = “
Prompt for y, with reminder
07
in 1
Input R1
08
lbl 0
Label 0, begin the loop
09
rcl 0

10
mul 2

11
sto 0
R0 * R2 → R0
12
rcl 1

13
sub 1.0
Keys: [ - ], [ = ], 1, [ = ]
R1 – 1
14
sto 1
R1 – 1 → R1
15
rcl 1
Put R1 in the display (accumulator)
16
gt 1.0
Keys: [ > ], [ = ], 1, [ = ]
Is R1 > 1?
17
gto 0
Goto Label 0 if R1 > 1
18
prt

19
prt “x^y = “
Start result string
20
out 0
Attach R0 (result) to string
21
eof


Examples:
Input:  x = 3, y = 5.  Result:  243
Input:  x = 2.7, y = 8.  Result: 2824.2953648100015

Modulus

This program calculates x mod y, where x and y are both positive and x > y.

Step
Line
Comment
00
prt
Clear the display
01
prt “x (>0) = “
Prompt for x
02
in 0
Input R0
03
prt

04
prt “y (>0) = “
Prompt for y
05
in 1
Input R1
06
lbl 0
Label 0, begin the loop
07
rcl 0

08
sub 1
Subtract R1
09
sto 0
R0 – R1 → R0
10
rcl 0

11
sub 1

12
ge 0.0
Keys: [ ≥ ], [ = ], 0 , [ = ]
R0 – R1 ≥ 0?
13
gto 0
Goto label 0, repeat loop if R0 ≥ R1
14
prt

15
prt “x mod y = “
Begin result string
16
out 0
Attach R0 to the result string
17
eof


Examples:
Input:  x = 44, y = 3.  Result:  44 mod 3 = 2
Input:  x = 76, y = 20.  Result:  76 mod 20 = 6

Final Verdict

The programming language seems to be clumsy at first and it will take some getting used to.  Turning on the sound and vibration made operating the app much better because the touch has to be precise.  I can see how this app can easily frustrate people. 

Also it is very unusual that the equals key is at the top row of the keyboard instead of the usual location at the bottom. 

With a lack of scientific functions, I recommend this app if:

(1) You want to engage mathematical programming from scratch.  Remember that all the scientific, financial, and advanced features had to be originally programmed using the four arithmetic functions themselves!
(2)  You want a challenge.

Eddie



This blog is property of Edward Shore, 2017.

RPN HP 12C: Fibonacci and Lucas Sequences

  RPN HP 12C: Fibonacci and Lucas Sequences Golden Ratio, Formulas, and Sequences Let φ be the Golden Ratio: φ = (1 + √5) ÷ 2...