In Python Are Dictionaries Much Slower in Performance Compared to Lists or Tuples?

Problem scenario
You want to know how dictionaries perform as iterables in Python. In Python for printing the values of every key-value pair in a dictionary, is it faster or slower than printing every item in a list? How does it compare to a tuple?

Solution
For this example we use integer keys in the dictionary. Keys can be strings or other objects.

 » Read more..

How Do You Upgrade to Python 3.x on Ubuntu 16?

Problem scenario
You are using Python 2.7.12 on Ubuntu 16. You want to upgrade to Python 3.x You are having problems. You tried several things as follows:
sudo apt-get -y install python 3.7
But you see this:

python is already the newest version (2.7.12-1~16.04).
python set to manually installed.
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.

 » Read more..

How Do You Troubleshoot “Error: Could Not Create the Java Virtual Machine”?

Problem scenario
You ran a Hadoop command but you receive this error:

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

What do you do?

Solution
Run hadoop help. This error can happen when you have an incorrect flag.

 » Read more..

How Do You Write a Python Program to Accept Input in a Non-interactive Way at Run-time?

Problem scenario
You are used to using “input” with Python 3 to prompt the user for a value. How do you pass input when you execute a Python program?

Solution
1. Use import sys

2. Use sys.argv[1] to refer to the first argument passed when you run your program like this:

python3 goodprog.py foo

The program will see “sys.argv[1]” as the string foo.

 » Read more..

How Do You Upgrade Python 2.x to Python 3.7 in Debian or Ubuntu Linux?

Problem scenario
You have a virtual server running Ubuntu 18.x in Azure. It has Python 2.x. How do you upgrade to Python 3.x?

Solution
1. Run these two commands:
sudo apt -y update
sudo apt -y install python 3.7

2. Find where the binary python3.7 is by running this: sudo find / -name python3.7
Make a mental note of the result.

 » Read more..

How Do You Write a Tic-Tac-Toe Program in Ruby?

Problem scenario
You want to write a Tic-Tac-Toe program in Ruby. What should you do?

Solution

# Tic-tac-toe game in Ruby. Written by continualintegration.com.
# We know Ruby is object-oriented. Ideally we will re-write this to encapsulate all logic in objects.
puts “This is a two player game of tictactoe. One person can pretend to be the other player.”
puts “Both players should share a keyboard and monitor.”
puts “The legend for squares in the grid is as follows: ”
puts ” ”
puts “***************************************************************”
puts “ltc is left-top-corner,  » Read more..

How Do You Prepare for a Job Interview as a Software Engineer?

Question
How do you prepare for a job interview as a software engineer?

Answer
Buy books on technical aspects of coding or one that is geared toward technical questions that may be asked. Here are options:

 » Read more..

How Do You Find the Largest and Smallest Numbers in a Python List in O(N^2) Time?

Problem scenario
You want to write a Python program that will accept a list of integers interactively. You want the program to find and print out the largest number and the smallest number of the list entered. You do not want to use the max() or min() functions (which work in Python 3 without importing any library).

What do you do?

Solution

def extremefinder(mainlist):
a = len(mainlist) – 1
b = len(mainlist)
maxnum = 0
minnum = 0
for x in mainlist:
low = 0
high = 0
for y in mainlist:
if (x y):
low = low + 1
if (low == a): # This never evaluates if x == y.  » Read more..

How Do You Install pip on a RHEL Server in AWS?

Problem scenario
You want to install pip on a RedHat Enterprise Linux server in AWS.  What do you do?

Solution
Prerequisites
This assumes that Python has been installed. If it has not, run this command:
sudo yum -y install python3


Procedures

Run these two commands:
curl “https://bootstrap.pypa.io/get-pip.py” -o “get-pip.py”
sudo python3 get-pip.py # use python is python3 is not found
sudo ln -s /usr/local/bin/pip /usr/bin/pip

 » Read more..

How Do You Troubleshoot the boto Message “NoRegionError”?

Problem scenarios (one or both of the following are happening)
#1 You are getting “botocore.exceptions.NoRegionError: You must specify a region” when you run a Python program (that involves boto and AWS).

#2 You are trying to create a session with a Python/boto program. You are printing out the Session information in your Python program using boto but you see this:

Session(region_name=None)

What should you do?

 » Read more..