vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The documentation for graphics.py is at
Click Here

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

This might be a hint:

'''zg_triangle1.py
using the Zelle graphics module (derived from Tkinter)
http://mcsp.wartburg.edu/zelle/python/graphics.py
'''

from graphics import *

def drawTrianglePatch(win, colour):
    # polygon goes back to starting point (80, 20)
    triangle = Polygon(Point(80,20),Point(180,60),Point(60,220))
    triangle.setFill(colour)
    triangle.draw(win)

w = 250
h = 250
win = GraphWin("triangle stuff", w, h)

colour = 'red'
drawTrianglePatch(win, colour)

# pause for click inside window, then exit
win.getMouse()
win.close()
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Recursive functions have a neat look, but consume much stack space, Python sets a recursion limit of 1000. Recursive functions are rather slow, a while/for loop is much faster.

Here are some typical examples:

def get_factorial1(n):
    '''
    would be poor choice because of the many function calls
    each call has stack overhead, a while/for loop is much faster
    '''
    # base case, also exit condition
    if n <= 1:
        return 1
    # recursive case (return is needed)
    return n * get_factorial1(n - 1

def get_factorial3(n):
    '''
    avoids recursion and uses a faster for loop
    a factorial is 1*2*3*4*5*6*7* ...
    '''
    # base case 0! = 1 and 1! = 1
    if n <= 1:
        return 1
    factorial = 1
    for x in range(1, n+1):
        factorial *= x
    return factorial
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You might want to look at third party module pandas (it is a free download)
http://pandas.pydata.org/pandas-docs/stable/tutorials.html#pandas-cookbook

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

"The world is a dangerous place, not because of those who do evil, but because of those who look on and do nothing."

Albert Einstein

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The algorithm used here works best wirth PNG or BMP image files. Image file formats that use compression that leads to a loss in picture quality are not suitable.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The problem with the code in
https://codegolf.stackexchange.com/questions/4707/outputting-ordinal-numbers-1st-2nd-3rd#answer-4712
is that the suffix for mumbers 11, 12, 13 is wrong.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Here is another example I wrote a while ago:

''' urlretrieve_image1.py
retrieve and save an image from a web page
using ...
urlretrieve(url[, filename[, reporthook[, data]]]) 
tested with Python27 and Python36  by  vegaseat
'''

try:
    # Python2
    from urllib import urlretrieve
except ImportError:
    # Python3
    from urllib.request import urlretrieve

# find yourself an image on an internet web page you like
# (MSW: right click on the image, look under properties and copy the address)
image_url = "http://www.google.com/intl/en/images/logo.gif"

# extract the filename from the url
url_list = image_url.split('/')
print(url_list)  # test
image_filename = url_list[-1]

# retrieve the image from the url and save it to a file in the working directory
urlretrieve(image_url, image_filename)

print("image saved as %s"  % image_filename)

# try to show the saved image file ...
import webbrowser
webbrowser.open(image_filename)
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

BTW, if the sequence is very large use a generator expression. Hint ...

# using a generator expression to sum directly
t2000 = sum(3**k for k in range(2000))
print("sum of first 2000 numbers = {}".format(t2000))
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Very nice, thanks martineau!
Got to testdrive your ideas. Right now I am working with an ACER Chromebook 15 and doing Python code is a little tough.
Will get to my trusty iMac soon.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Is this because Smalltalk is totally Object Oriented?
With Python you can use a number of different programming styles.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

@martineau: Since you are the expert in good Python programming, what is your solution? Show us what you got!

rproffitt commented: “SHOW ME WHAT YOU GOT! I WANT TO SEE WHAT YOU GOT!" - Cromulons. +15
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Try to get the sequence of numbers into a list using a for loop.
Then you can sum up the elements of the list with sum()

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I could think of valid functions that perform an action, like formated printing. No need to return anything.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You are so right, I thought since 2 month had gone since the original request the whole thing became a teaser for the mind.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
s = "123456789"

for k in range(1, 10):
    print(s[-k:])

''' output...
9
89
789
6789
56789
456789
3456789
23456789
123456789
'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Python has a nice thing called sequence slicing:
[starting-at-index : but-less-than-index [ : step]]
'start' defaults to 0, 'end' to len(sequence), 'step' to 1

for instance
s = "123456789"
then
s[-1:] --> 9
s[-2:] --> 89
s[-3:] --> 789
and so on,
do this with a for loop in range(1, 10)

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Give your function an argument, in this case just an empty one.

def exit(event):

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

GUI (Graphics User Interface) programs use a loop to check and update the information the program receives. With tkinter the loop is called mainloop.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Here is the most basic approach ...

sentence = "Hello, my name is Mattia"

# default split is at whitespace
for word in sentence.split():
    print(word)

''' result ...
Hello,
my
name
is
Mattia
'''

I leave it to you to separate the comma from Hello.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Some posters are rude because they don't know any better. For instance they grew up in a home/society where everything was handed to them.

My solution, give them an answer that needs work.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

If your program is any good, then sooner or later someone will simply write a better program without reverse engineering.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You can claim, but the rub is that you have to be willing to defend your claim in a court of law at great expense.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Maybe this keeps mass shootings away from Kentucky ...

From January 1 to September 30, 2015, the US Federal Bureau of Investigation (FBI) conducted more than 2.3 million background checks on Kentuckians buying guns, along with nearly 1.2 million checks in California and roughly 1.1 million in Texas. Kentucky’s population currently numbers 4.4 million, which means the FBI carried out one firearms background check for roughly every two Kentucky residents. Just to remind you, background checks are supposed to keep guns and idiots apart.

Source ...
http://www.washingtonmonthly.com/republic3-0/2015/10/which_states_have_the_most_gun057962.php

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Asylum seekers in Germany complaints:
The free food is bad.
Not enough free spending money.
The bathrooms stink.
The free rooms are cold.

German complaints about their guests:
Most don't know how to use a toilet.

Picked up from various news sources (mostly BBC).

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

About the size of the National Debt of the United States in another 20.7 years.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Germany spends an estimated 1000 Euros on each asylumseeker per month. They expect about one million new asylumseekers this year alone, more than that next year. Many arrive without any papers, so the approval process takes up to 2 years. Luckily, Germany is a very rich country and gladly spends the money.

Once given asylum status the person can compete with the local workforce. So all those engineers from Syria and software experts from Somalia can prove themselves. If the status is denied, you can simply stay on with the present support system, but you can not work. Sweden is similar to that, but their support system is much more generous.

I think the Germans are hoping that their rejects will over the years all end up in Sweden.
Source: BBC

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Of course you can study algorithms first, but you have to be aware that a particular pet algorithm may not be stable or very efficient in the computer language you use later. The language you are coding in is usually picked by your employer, not you.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Actually, the chicken came first then the egg.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

To get a GIF picture from the Internet ...

''' Tk_Canvas_Image_url.py
display an image obtained from an internet web page in Tkinter
tested with Python27 and Python34  by  vagaseat   21nov2012
'''

import io
import base64
try:
    # Python2
    import Tkinter as tk
    from urllib2 import urlopen
except ImportError:
    # Python3
    import tkinter as tk
    from urllib.request import urlopen


root = tk.Tk()
root.title("display a website image")
# a little more than width and height of image
w = 520
h = 320
x = 80
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

# this GIF picture previously downloaded to tinypic.com
image_url = "http://i46.tinypic.com/r9oh0j.gif"

image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)
photo = tk.PhotoImage(data=image_b64)

# create a white canvas
cv = tk.Canvas(bg='white')
cv.pack(side='top', fill='both', expand='yes')

# put the image on the canvas with
# create_image(xpos, ypos, image, anchor)
cv.create_image(10, 10, image=photo, anchor='nw')

root.mainloop()
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Math with hexadecimal numbers ...

hx1 = "FF"
hx2 = "B7"
product = int(hx1, 16) * int(hx2, 16)

print("{} * {} = {} --> {:X}".format(hx1, hx2, product, product))

'''
FF * B7 = 46665 --> B649
'''
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Python function int() will do this for you ...

hx = "FFFF"
# convert hexadecimal string hx to a denary (base 10) number
n = int(hx, 16)

print(n)  # 65535
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Swift2 has come upon us bringing along a fair number of changes. I have taken the original Swift 1.2 snippet and run it through the migration utility. Just like in Python2 to Python3, print() is the obvious change, but there are some rather subtle changes too ...

//
//  main.swift
//  Taste_of_swift1
//
// exploring Apple's Swift language, part 1 
// as of Swift 2, String no longer conforms to CollectionType
// to get things to work s.characters is used a lot
// modified to work with Swift2  vegaseat   18sep2015

import Foundation

// variables ...

// use 'let' to create a constant (cannot be changed later)
let Pi = 3.14

// use 'var' for variables (type is by inference)
var n = 30
// if you want value 30 to be a floating point value
// use Float (32bits) or Double (64bits)
var q: Double = 30
// or
var r = 30.0

// strings ...

// in Swift String is a structure
// a String behaves like a doubly linked list of characters
// rather than an array, string handling is more complex, but
// it makes working with Unicode less error-prone
// strings are enclosed in ""
// there is no multiline string syntax (bummer!)
var s = "hippopotamus"
print(s.hasPrefix("hip"))  // true
print(s.hasSuffix("mud"))  // false
print(s.lowercaseString)   // hippopotamus
print(s.uppercaseString)   // HIPPOPOTAMUS
print(s.isEmpty)           // false
print(s.startIndex)        // 0

// gives the length of String s
print(s.endIndex)             // 12
print(s.characters.count)     // 12

// gives the actual end index value …
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

One of the many ways ...

table =[
["_","_","_"],
["_","_","_"],
["_","_","_"]
]

header = [1, 2, 3]
sf = "{} {} {}"
print(sf.format(*header))
for line in table:
    print(sf.format(*line))
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

An old cowboy dressed to kill with cowboy shirt, hat, jeans, spurs and
chaps went to a bar and ordered a drink. As he sat there sipping his whiskey,
a young lady sat down next to him.

After she ordered her drink she turned to the cowboy and asked him,
"Are you a real cowboy?"
To which he replied, "Well, I have spent my whole life on the ranch, herding cows, breaking horses, mending fences, I guess I am."

Then he asked her what she was. She replied, "I've never been on a ranch
so I'm not a cowboy, but I am a lesbian. I spend my whole day thinking
about women. As soon as I get up in the morning I think of women, when I
eat, shower, watch TV, everything seems to make me think of women."

A short while later she left and the cowboy ordered another drink. A
couple sat down next to him and asked, "Are you a real cowboy?"
To which he replied, "I always thought I was, but I just found out that I'm a lesbian."

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

A further look at the memory locations ...

>>> a = 10.1
>>> b = 10.1
>>> a is b
False
>>> c = 10.1; d = 10.1
>>> c is d
True
>>> id(a)
4298630584
>>> id(b)
4298631760
>>> id(c)
4298630704
>>> id(d)
4298630704
>>>

c = 10.1; d = 10.1 behaves more like c = d = 10.1

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Should work, good idea.
I am surprised that anybody still uses these snippets. I have stopped writing them because there was so little interest.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Prove that
(-1/2)! equals sqrt(pi)

Nutster commented: How is this a programming exercise? This is a property of the Gamma function. +0
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Frank Gifford, America's most beloved sports entertainer has passed away in an untimely manner. A great loss for the country and the world.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I keep reading that parts of Europe are experiencing a sizzling summer with record heat. I wonder if other parts of the world notice in increase in heat?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

"A politician is a fellow who will lay down your life for his country."
... Tex Guinan

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

"Replacing the Judicial System with a Computer Database"

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

There might be a lot of "nationalnets" rather than one "internet".

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Due to political correctness a 'DUMB BLONDE' is now a
'LIGHT-HAIRED DETOUR OFF THE INFORMATION SUPERHIGHWAY'.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

The Mafia is organized crime.
The Government is disorganized crime.
... Stefan Molyneux

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

It would only go away if "something" better came along. Maybe finally "something" the hackers couldn't abuse at will.

I just got a letter from the UCLA hospital that their database got hacked/stolen with all their patients (present and past) private information on it! I could do without this grap!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You can pay DropBox a few extra bucks to add some capacity. That is the idea to get you started for free. Google, MS and Apple all do this. A totally free service will not survive on the long run.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Coming in the fall of 2015 ...
Swift2 replaces println() with just print() that has a newline default. The sort of thing that Python3 did to print(). There are some other syntax changes.
Good news, there will be a Swift 1-to-2 migrator utility to change Swift1 syntax to Swift2 syntax.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Coming in the fall of 2015 ...
Swift2 replaces println() with just print() that has a newline default. The sort of thing that Python3 did to print(). There are some other syntax changes.
Good news, there will be a Swift 1-to-2 migrator utility to change Swift1 syntax to Swift2 syntax.