Przeglądaj źródła

Initial commit (Strängar & Funktioner

Viktor Grahn 7 lat temu
commit
9802ed2386
14 zmienionych plików z 331 dodań i 0 usunięć
  1. 13 0
      Funktioner/1.py
  2. 25 0
      Funktioner/2.py
  3. 24 0
      Funktioner/3.py
  4. 23 0
      Funktioner/4.py
  5. 39 0
      Funktioner/5.py
  6. 13 0
      Funktioner/6.py
  7. 24 0
      Funktioner/7.py
  8. 10 0
      Strangar/1.py
  9. 10 0
      Strangar/2.py
  10. 22 0
      Strangar/3.py
  11. 37 0
      Strangar/4.py
  12. 25 0
      Strangar/5.py
  13. 30 0
      Strangar/6.py
  14. 36 0
      Strangar/7.py

+ 13 - 0
Funktioner/1.py

@@ -0,0 +1,13 @@
+import os
+
+def printName():
+    "Prints the name Viktor Grahn"
+    print("Viktor Grahn")
+    return
+
+# Clear screen
+os.system("clear")  # Mac or Linux
+# os.system("cls")  # Windows
+
+# Call to function printName()
+printName()

+ 25 - 0
Funktioner/2.py

@@ -0,0 +1,25 @@
+import os
+
+def printLowest():
+    "Takes to numbers from prompt and prints the lowest of them"
+    # Assign the prompted numbers to variables x and y
+    x = int(input("Enter a number: "))
+    y = int(input("Enter another number: "))
+
+    # Test if x is lesser than y
+    if x < y:
+        print(x)
+    # Test if y is lesser than x
+    elif y < x:
+        print(y)
+    # If previous conditionals weren't met, assume equal values
+    else:
+        print("Both numbers are ", x)
+    return
+
+# Clear screen
+os.system("clear")  # Mac or Linux
+# os.system("cls")  # Windows
+
+# Call to function printLowest()
+printLowest();

+ 24 - 0
Funktioner/3.py

@@ -0,0 +1,24 @@
+import os
+
+def printLargest(x, y, z):
+    "Takes three numbers and prints the largest of them"
+    # Initiate variable to hold largest number and assign it the first number
+    largest = x; 
+
+    # Iterate over passed numbers
+    for number in (x, y, z):
+        # Test if current number is larger than value stored in variable
+        # If so, replace value of variable
+        if number > largest:
+            largest = number
+
+    # Print value stored in variable
+    print(largest)
+    return
+
+# Clear screen
+os.system("clear")  # Mac or Linux
+# os.system("cls")  # Windows
+
+# Call to function printLargest with three parameters
+printLargest(42, 12, 900)

+ 23 - 0
Funktioner/4.py

@@ -0,0 +1,23 @@
+import os
+
+def divideableBy3(number):
+    "Check if passed number is divideable by three"
+    # Use modulo to check if number is divideable by three
+    divideable = (number % 3 == 0)
+
+    # Return bool variable
+    return divideable
+
+# Clear screen
+os.system("clear")  # Mac or Linux
+# os.system("cls")  # Windows
+
+# Prompt for input from user
+# Use int to convert input to integer
+number = int(input("Input a number: "))
+
+# Call function divideableBy3 and test return value (bool)
+if divideableBy3(number):
+    print("{} is divideable by 3".format(number))
+else:
+    print("{} is not divideable by 3".format(number))

+ 39 - 0
Funktioner/5.py

@@ -0,0 +1,39 @@
+import os
+
+def divideableBy3(number):
+    "Check if passed number is divideable by three"
+    # Use modulo to check if number is divideable by three
+    divideable = (number % 3 == 0)
+
+    # Return bool variable
+    return divideable
+
+def divideableBy2(number):
+    "Check if passed number is divideable by two"
+    # Use modulo to check if number is divideable by two
+    divideable = (number % 2 == 0)
+
+    # Return bool variable
+    return divideable
+
+# Clear screen
+os.system("clear")  # Mac or Linux
+# os.system("cls")  # Windows
+
+# Prompt for input from user
+number = int(input("Input a number: "))
+
+# Call function divideableBy2 and store return in variable test2
+test2 = divideableBy2(number)
+# Call function divideableBy3 and store return in variable test23
+test3 = divideableBy3(number)
+
+# Test results from function calls, and base output on the results
+if test2 and test3:
+    print("{} is divideable by 2 and 3".format(number))
+elif test2:
+    print("{} is divideable by 2".format(number))
+elif test3:
+    print("{} is divideable by 3".format(number))
+else:
+    print("{} is not divideable by 2 or 3".format(number))

+ 13 - 0
Funktioner/6.py

@@ -0,0 +1,13 @@
+import os
+
+def calcRectangleCircumference(x, y):
+    "Calculates area of a rectangle based on passed width and height"
+    area = (x * 2) + (y * 2)
+    return area
+
+# Clear screen
+os.system("clear")  # Mac or Linux
+# os.system("cls")  # Windows
+
+# Call function calcRectangleCircumference and print results
+print(calcRectangleCircumference(2,3))

+ 24 - 0
Funktioner/7.py

@@ -0,0 +1,24 @@
+import os
+
+def fibonacci(threshold):
+    # Assign a start value for sequence
+    current = 1
+    previous = 0
+
+    # Iterate fibonacci sequence as long as value is lesser than x
+    while current <= threshold:
+        # Print current value
+        print(current)
+        # Calculate new value from current value + previous value
+        new = current + previous
+        # Store the current value for next iteration
+        previous = current
+        # Store the new value as the current value
+        current = new
+
+# Clear screen
+os.system("clear")  # Mac or Linux
+# os.system("cls")  # Windows
+
+# Call function fibonacci()
+fibonacci(1024)

+ 10 - 0
Strangar/1.py

@@ -0,0 +1,10 @@
+import os
+
+# Clear screen
+os.system("clear") # Mac or Linux
+#os.system("cls") # Windows
+
+userInput = input("Input a string: ")
+
+# Use len function to determine length of userInput
+print("User input", userInput, "is", len(userInput), "characters long.")

+ 10 - 0
Strangar/2.py

@@ -0,0 +1,10 @@
+import os
+
+# Clear screen
+os.system("clear") # Mac or Linux
+#os.system("cls") # Windows
+
+userInput = input("Input a longer string: ")
+
+# Print substring, from start to position 3 of userInput
+print(userInput[:3])

+ 22 - 0
Strangar/3.py

@@ -0,0 +1,22 @@
+import os
+
+# Clear screen
+os.system("clear") # Mac or Linux
+#os.system("cls") # Windows
+
+userInput = input("Input a string with both letters and digits: ")
+
+# Variables for counting letters and numbers
+letters = 0
+digits = 0
+
+# Iterate string character by character
+for i in userInput:
+    # If character is a letter, increase letters counter
+    if i.isalpha():
+        letters += 1
+    # If character is a number, increate number counter
+    elif i.isdigit():
+        digits += 1
+
+print("User input has", letters, "letters and", digits, "digits in it.")

+ 37 - 0
Strangar/4.py

@@ -0,0 +1,37 @@
+import os
+# Module to hide password input
+import getpass
+
+# Clear screen
+os.system("clear") # Mac or Linux
+#os.system("cls") # Windows
+
+# Get user input (will not show in terminal upon input)
+userInput = getpass.getpass("Input a password: ")
+
+# Reset criteria to unfulfilled
+letterCriteria = False
+numberCriteria = False
+lengthCriteria = False
+
+# Check length criteria (>= 12 characters long)
+# Store true or false in variable lengthCriteria
+lengthCriteria = len(userInput) >= 12
+
+# Iterate the password, character by character
+for i in userInput:
+    # Test if character is alpha
+    # If so, set letterCriteria to true (critera fulfilled)
+    if i.isalpha():
+        letterCriteria = True
+
+    # Test if character is digit
+    # If so, set numberCriteria to true (critera fulfilled)
+    if i.isdigit():
+        numberCriteria = True
+
+# Check if all critera is fulfilled (variables are True)
+if letterCriteria and numberCriteria and lengthCriteria:
+    print("Password fulfilled all the criteria.")
+else:
+    print("Password must contain at least one letter and one digit, and contain at least 12 characters.")

+ 25 - 0
Strangar/5.py

@@ -0,0 +1,25 @@
+import os
+
+# Clear screen
+os.system("clear") # Mac or Linux
+#os.system("cls") # Windows
+
+def contains(string, char):
+    "Check if a string contain a single character"
+    # Iterate the string, character by character
+    for i in string:
+        # If current character matches provided character, stop and return True
+        if i == char:
+            return True
+
+    # At the end, if no match was found; return False
+    return False
+
+userInputString = input("Input a string: ")
+userInputChar = input("Input a single char: ")
+
+# Call contains function and output results
+if contains(userInputString, userInputChar):
+    print(userInputString, "does contain", userInputChar)
+else:
+    print(userInputString, "does not contain", userInputChar)

+ 30 - 0
Strangar/6.py

@@ -0,0 +1,30 @@
+import os
+
+def printReverse(string):
+    # Get length of string
+    stringLength = len(string)
+
+    # Variable holding the new reversed string
+    newString = ""
+
+    # Position of first letter is considered 0 in string
+    # Set i to position of last letter in string
+    i = stringLength - 1
+
+    # Iterate the string backwards (count backwards from length of string down to zero)
+    while i >= 0:
+        # Add character of position i to new reversed string
+        newString = newString + string[i]
+        # Decrease counter by 1
+        i -= 1
+
+    print(newString)
+
+# Clear screen
+os.system("clear") # Mac or Linux
+#os.system("cls") # Windows
+
+userInput = input("Input a string: ")
+
+# Call printReverse function
+printReverse(userInput)

+ 36 - 0
Strangar/7.py

@@ -0,0 +1,36 @@
+import os
+
+def isPalindrome(string):
+    "Check if a string is a palindrome"
+    # Get length of string
+    stringLength = len(string)
+
+    # Variable holding the new reversed string
+    newString = ""
+
+    # Position of first letter is considered 0 in string
+    # Set i to position of last letter in string
+    i = stringLength - 1
+
+    # Iterate the string backwards (count backwards from length of string down to zero)
+    while i >= 0:
+        # Add character of position i to new reversed string
+        newString = newString + string[i]
+        # Decrease counter by 1
+        i -= 1
+
+    # Compare new reversed string with original string, and return result
+    # Make all letters lower case to enable a correct comparison
+    return string.lower() == newString.lower()
+
+# Clear screen
+os.system("clear") # Mac or Linux
+#os.system("cls") # Windows
+
+userInput = input("Input a string: ")
+
+# Call isPalindrome function and print output based of result
+if isPalindrome(userInput):
+    print(userInput, "is a palindrome.")
+else:
+    print(userInput, "is not a palindrome.")