<jupyter_start><jupyter_text>[![PALS0039 Logo](https://www.phon.ucl.ac.uk/courses/pals0039/images/pals0039logo.png)](https://www.phon.ucl.ac.uk/courses/pals0039/)Exercise 1.1 Python PracticeIn this exercise we practise writing some Python code to implement some simple functions. (a) Write a function that returns the Greatest Common Divisor of two integers and test it out with some examples, for instance:*   GCD(50,15) => 5*   GCD(80,256) => 16[Euclid's algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm) for finding the GCD of two integers greater than zero is as follows:* if a equals b, the answer is a* if a is bigger than b the answer is the GCD of a-b and b* if b is bigger than a the answer is the GCD of a and b-a**Hint:** You will want to define a function **GCD(a,b)** that tests the value of **a** compared to **b** and returns one of three values.<jupyter_code>def GCD(a,b):
  """GCD calculates the Greatest Common Divisor of two integers greater than zero using Euclid's algorithm"""

  if a == b:
    return a
  elif a>b:
    return GCD((a-b),b) 
  else:
    return GCD((b-a),a)

print(GCD.__doc__)
print(50, 15, GCD(50, 15))
print(80, 256, GCD(80, 256))<jupyter_output><empty_output><jupyter_text>---(b) Write a function that tests whether an integer is prime (has no factors other than itself and 1). Test the function by printing all the primes less than 1000.[A number is prime](https://en.wikipedia.org/wiki/Primality_test) if there are no numbers greater than 1 and less than or equal to the square root of the number that divide into the number without a remainder.**Hint:** You will want to define a function **isprime(n)** that tests whether any integers greater than 1 but smaller than **n** divide into it with no remainder. The Python operator **%** gives you the remainder after division. You can use a **for** loop to print all the primes up to 1000.<jupyter_code>from math import sqrt

def isprime(n):
  """isprime tests whether an integer is prime (has no other factors than itself and 1)"""
  for i in range(2,int(sqrt(n))+1):
    if (n % i == 0):
      return False
    return True

primes = [num for num in range(1000) if isprime(num)]

print(primes)<jupyter_output><empty_output><jupyter_text>---(c) Write a program that inputs a string from the user then splits the string into separate words. Print out the separate words in normal order, in reverse order and in sorted order.**Hint:** The **input()** function will read a string from the keyboard. The string method **split()** will divide a string at instances of a separator character. The array slicing notation can be used to select sections of the array of words. You will find additional help in the documentation of the [Python string class](https://docs.python.org/3.5/library/stdtypes.htmlstring-methods).<jupyter_code><jupyter_output><empty_output><jupyter_text>---(d) Write a program that inputs a string from the user, splits the string into separate words then makes the following substitutions:* I => you* me => you* my => your* mine => yours* . => ?For example:* input: "I like my book."* output: "You like your book?"**Hint:** Once you have the input from the user as an array of words, you can loop through it making substitutions. Then you can combine the array back into a single string with the string **join()** method. You can make substitutions with a chain of **if** statements, but a better way would be to use a dictionary - if a word is a key in the dictionary, then substitute it for the keyed value.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09816462736373749, 'fraction_numerical': 0.01918798665183537, 'mean_word_length': 4.409022556390977, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '132254', 'license': 'MIT', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1006, 'n_words': 503, 'path': 'Exercise_1_1.ipynb', 'repo_name': 'grendelaglaeca/dw_matrix', 'size': '3596'}