...The Answer to the Ultimate Question of Life, The Universe, and Everything:
In [1]:
As_soon_as_I_become_a_good_programmer_I_will_be_rich=False
# Let us test it :
if As_soon_as_I_become_a_good_programmer_I_will_be_rich: print("Then let us start programming !")
else: print('Do Algorithmics !')
a palindroma is a character's string containing a chain of lenght n
"abc...cba" is a palindroma
"aba" is a palindroma
any word with two identical letters is a palindroma
In [2]:
## Check if the french word "ressasser" is a palindroma
# We first declare some variables
word_to_test='ressasser'
# We need to define the indexes in the chain of characters
index_head=0
index_tail=len(word_to_test)-1
# Now we are going to print
print(word_to_test)
print("The lenght of the word is: {}".format(len(word_to_test)))
print("The first letter is: {}".format(word_to_test[index_head]))
print("The last letter: {}".format(word_to_test[index_tail]))
In [3]:
if (word_to_test[index_head]==word_to_test[index_tail]):
if index_head < index_tail:
print("Untill now it seems a palindroma")
index_head=index_head+1
index_tail=index_tail-1
print(index_head)
print(index_tail)
else: print("The word '{}' is a palindroma".format(word_to_test))
else: print("The word '{}' is NOT a palindroma".format(word_to_test))
In [4]:
# Give a number and find the first guess
print('Enter the number y, from whom you are asked to find the square root')
y=eval(input('y = '))
print('Give a first guess for x, called g')
g=eval(input('g = '))
print("End of step 1")
In [5]:
print("(The square product of {0} is {1})".format(g,g*g))
print("The square of the average of the guess and y divided by guess is:")
print((0.5*(g+y/g))**2)
In [6]:
print("How accurate is this result ?")
print("Well, the square of your guess is {}".format(g*g))
if g*g == y:
print("Whoah, it is a good guess")
print("The solution is:")
print((0.5*(g+y/g)))
else:
g=0.5*(g+y/g)
print("Sorry")
print("Try again, with, g={} as new guess".format(g))
print("The average of the new g and y divided by this new g is:")
print(0.5*(g+y/g))
In [7]:
#Check a number
nb = eval(input('Enter a number = '))
if nb < 0:
print('{} is a negative number'.format(nb))
elif nb > 0:
print('{} is a positive number'.format(nb))
else:
print('{} is a null'.format(nb))
In [8]:
# Leap years are back all 4 years, except secular years if these are not multiple of 400
n = eval(input('Enter a year = '))
if n % 4 != 0:
print('Year {} is NOT a bissextile year'.format(n))
elif n % 100 != 0:
print('Year {} is a bissextile year'.format(n))
elif n % 400 != 0:
print('Year {} is NOT a bissextile year'.format(n))
else:
print('Year {} is a bissextile year'.format(n))
In [9]:
if ((n % 4 == 0) and (n % 100 != 0)) or (n % 400 == 0): print('Year {} is a bissextile year'.format(n))
else: print('Year {} is NOT a bissextile year'.format(n))
Putting statements x and y in relations trough the operators is the basics of formal logic
Sign | Meaning |
---|---|
x == y | x Is equal to y |
x != y | x Is Not equal y |
x > y | x Is greater than y |
x < y | x Is smaller than y |
x >= y | x Is greater or equal than y |
x <= y | x Is smaller or equal than y |
-x | opposite to x (when x a number) |
+x | positive to x (when x a number) |
~x | symmetric to x (when x a number) |
In [10]:
a=6
print(~a)
In [11]:
# There is an operator for defining an 'implication'
# p implies q means that
# p is a sufficient condition for q to be true
# p --> q is equivalent as (not(p) or q)
Je_fais_de_longues_etudes=True
Je_deviens_bon=False
# Je_fais_de_longues_etudes --> Je_deviens_bon
# This can be writen as the following proposition :
(not(Je_fais_de_longues_etudes) or (Je_deviens_bon))
Out[11]:
In [12]:
a=10
b=3
c=a // b
d=a % b
print(a)
print(type(a))
print(b)
print(type(b))
print(c)
print(type(c))
print(d)
print(type(d))
In [13]:
'y'*3
Out[13]:
In [14]:
a='Je ne dois pas consulter Facebook en cours' + '//'
a*5
Out[14]:
In [15]:
a = 5
b = 20
(a < b)
Out[15]: