External Exam Download Resources Web Applications Games Recycle Bin

Exercise 6 - Watch List - Solution

energyEfficientCryptos = [
  "SolarCoin",
  "Chia",
  "Cardano",
  "Stellar",
  "Harmony"
]

exchangeRates = [
  ("Bitcoin", 65460.31),
  ("Ethereum", 5272.41),
  ("Cardano", 1.87),
  ("Stellar", 0.38),
  ("Dogecoin", 0.24),
  ("Ripple", 1.19),
  ("Harmony", 0.34)
]

BEGIN checkEnergyEfficiency(cryptocurrency)
  match = False
  FOR count = 0 to LENGTH(energyEfficientCryptos)
    IF cryptocurrency == energyEfficientCryptos[count] THEN
      match = True
      BREAK #optional, improves efficiency
    ENDIF
  NEXT count
  ENDFOR
  IF match THEN
    OUTPUT "energy efficient"
    RETURN True
  ELSE
    OUTPUT "not energy efficient"
    RETURN False
  ENDIF
END

BEGIN measureCost(cryptocurrency, exchangeRate)
  IF exchangeRate <= 1.50 THEN
    OUTPUT "cost effective"
    RETURN True
  ELSE
    OUTPUT "too expensive"
    RETURN False
  ENDIF
END

BEGIN
  FOR count = 0 to LENGTH(exchangeRates)
    crypto = exchangeRates[count]
    cost = exchangeRates[count]
    OUTPUT "Checking", crypto
    costEffective = measureCost(crypto, cost)
    energyEfficient = checkEnergyEfficiency(crypto)
    IF costEffective AND energyEfficient THEN
      OUTPUT "purchase"
    ELSE
      OUTPUT "do not purchase"
    ENDIF
  NEXT count
  ENDFOR
END
energyEfficientCryptos = [
  "SolarCoin",
  "Chia",
  "Cardano",
  "Stellar",
  "Harmony"
]

exchangeRates = [
  ("Bitcoin", 65460.31),
  ("Ethereum", 5272.41),
  ("Cardano", 1.87),
  ("Stellar", 0.38),
  ("Dogecoin", 0.24),
  ("Ripple", 1.19),
  ("Harmony", 0.34)
]

def checkEnergyEfficiency(cryptocurrency):
  if cryptocurrency in energyEfficientCryptos:
    print(" - energy efficient")
    return True
  else:
    print(" - not energy efficient")
    return False

def measureCost(cryptocurrency, exchangeRate):
  if exchangeRate <= 1.50:
    print(" - cost effective")
    return True
  else:
    print(" - too expensive")
    return False

#MAIN ALGORITHM:
for (crypto, cost) in exchangeRates:
  print("Checking", crypto)
  costEffective = measureCost(crypto, cost)
  energyEfficient = checkEnergyEfficiency(crypto)
  if costEffective and energyEfficient:
    print(" - purchase")
  else:
    print(" - do not purchase")