How Do You Rotate a List in Python?

Problem Scenario
Rotating a list can make it appear circular (wherein an iteration from the last element brings you to the first element in the list). You do not want to import any modules. You have a list called foobar. You want to keep the content the same, but adjust the index by 1 on each value. How do you rotate a list in Python?

Solution

foobar = foobar[-1:] + foobar[:-1] …

How Do You Print Out Aggregate Cryptocurrency Information Based on a Date Range?

Problem scenario
You want to print out the average cryptocurrency price along with the minimum and maximum prices based on a range of dates. What do you do?

Solution
This solution only goes back to 2015 for BTC. For other cryptocurrencies, the oldest price data is usually more recent than that.

Prerequisite
This assumes you have installed pandas: pip install pandas
If you need assistance installing pip,

How Do You Write a Palindrome Testing Program in Python That Ignores Punctuation and Spaces?

Problem scenario
You want to write a palindromic testing program without importing any modules that ignores a string’s punctuation or spaces. What do you do?

Solution
Use this program:

def is_palindrome(s: str) -> bool:
# i moves forward, and j moves backward
i, j = 0, len(s) -1
while i < j:
while not s[i].isalnum() and i < j:
i += 1
while not s[j].isalnum() and i < …

How Do You Build a Flask Application to Return the Price of a Cryptocurrency?

Problem scenario
You want to write a Flask program to display the high and low prices of a given cryptocurrency for a given day. You want the output to include the volume too. How do you do this?

Solution
Prerequisite

This assumes that you have installed Flask and pandas.

Procedures
Run this program.

How Do You Use Boto and CloudFormation to Delete a Stack?

Problem scenario
You want to delete a CloudFormation Stack with Boto. What do you do?

Solution
Run a program like this (but replace “continualstack” with the name of the stack you deployed):

import boto3
cloudf = boto3.client(‘cloudformation’, region_name=’us-west-1′, aws_access_key_id=’AKIAMNOPQRST’, aws_secret_access_key=’FOOBAR1234/ZYXWV987654′)
json_stack=open(‘lampstack.json’, ‘r’).read()
cloudf.delete_stack(StackName=’continualstack’) …

How Do You Troubleshoot the Python Errors “NameError: name ‘copy’ is not defined” or “object has no attribute ‘copy'”?

Problem scenario
You are running a Python program. But you get “NameError: name ‘copy’ is not defined” or “object has no attribute ‘copy'”. What should you do?

Solution
Use “import copy” at the top of the program.

How Do You Write a Python Program to Find if The “Read more” Preview on a Web Page is Visible for Every Web Page Previewed?

Problem scenario
Sometimes the “Read more” or “Continue reading” hyperlink in the page preview of your website (e.g., when you search for a word), is not visible. You know that some of your web page previews on your website don’t have the “Read more” hyperlink at the end. This makes the preview appear to have the entire web page. You need to identify the postings that don’t have the “Read more.” How do you use Python to find the pages that have previews that have no “Read more” hyperlink?

How Do You Write a Python Function to Determine if There Are Three or More Unique Characters in a String?

Problem scenario
You want a function to tell you if there are three unique characters in a string. For example “ababababa” would have fewer than three unique characters. But “abc” would have three or more unique characters. How do you write a function that does this that solves in linear time?

Solution

variable_to_test = “aaaaaaaaaaaabbbbbb”

def two_unique(s):
flag = “There are only two unique characters in the string!”
dict_a = {}
for string_portion in s:
if string_portion in dict_a:
pass
else:
dict_a[string_portion] = 1
key_counter = 0
for key in dict_a.keys():
key_counter = key_counter + 1; …

Why Are Python List Operations/Methods Unlike Python String Operations/Methods?

Problem scenario
List operations such as .sort() will change previous copies of the list.

String operations such as .replace(“old_pattern”, “new_pattern”, count), will not change the variable or its copies; these operations will return a string after the replace operation has run on the original string.

Why is this behavior happening?

Solution
In-place changes and mutability are factors.