How Do You Write a Python Program to Test if Integers Are in Order?

Problem scenario
You need to write a Python program to see if a list of unique integers is in order. You can copy the list and sort it to compare the ideal state to the list provided. The program must print out a relevant message if a swap of two numbers can place those two numbers in the correct order. The program must print out if a given number is already in order.

Here are the requirements of the Python program:

  • generate a list of numbers (either by random or hard-code a list)
  • sort the list
  • find if incorrectly placed numbers share a spot with another number that if swapped, both would be in order

Solution

import random

def diff_finder(array):
  unsorted_list = array[:]
  array.sort()
  counter = 0
  for i in range(len(array)):
    if array[i] != unsorted_list[i]:
      if unsorted_list[array.index(unsorted_list[i])] == array[i]:   
      # if the integer in this position (the array.index(unsorted_list[i]) returns the position of the value of the wrong integer),
      # is the value of the correctly-ordered integer in the sorted array
        print("ONE SWAP WOULD CORRECTLY PLACE TWO NUMBERS!")
      else:
        print("It will take more than one swap with this number to get the list in order: " + str(unsorted_list[i]) )
    else:
      print(str(array[i]) + " is in correct order")

a_list = []

for i in range(10):
  a_list.append(random.randint(1,10))

print("This list we are dealing with is " + str(a_list))
diff_finder(a_list)

How Do You Configure Nginx to Listen to Messages Sent via HTTP?

Problem scenario
You have Nginx configured. The access logs never pick up any POST message content. You get 405 error messages. What do you do?

Solution
This will send a message payload -- but not via POST. It may not work for your situation. To get a message to be sent to a website (a listening web service), craft a URL that does not necessarily exist, but retain the domain name or IP address.

Run a command like this: curl http://x.x.x.x/SPECIALTESTTOAPPEAR
(But replace x.x.x.x with the domain name or the IP address of the website; replace the "SPECIALTESTTOAPPEAR" with the string of your choice.)

Go to the access.log file on the back-end of the Nginx server (e.g., run sudo find / -name access.log). You should see "SPECIALTESTTOAPPEAR" in the logs.

How Do You Solve the Kubernetes Error “networkPlugin cni failed to set up pod network: open /run/flannel/subnet.env: no such file or directory”?

Problem scenario
You run kubectl describe pods. You see a message in the output that says

networkPlugin cni failed to set up pod network: open /run/flannel/subnet.env: no such file or directory.

How do you fix this?

Possible Solution #1
Log into every worker node. Make sure it has /run/flannel/subnet.env with the following four lines:

FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true

(This solution was adapted from https://www.programmersought.com/article/7824768991/)

Possible Solution #2
Run this: kubectl get pods. It could be that the error message shows up, but there is no problem. If the problem has since subsided, it may be ignorable.

Possible Solution #3
"More often than not, there is another issue that could be causing this error that needs to be investigated first."
(This one was taken from this Microsoft website.)

How Do You Troubleshoot “/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:83:in `require’: cannot load such file — winrm-elevated (LoadError)”?

Problem scenario
You run a Vagrant command. But you get this message: "/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:83:in `require': cannot load such file -- winrm-elevated (LoadError)"

What should you do?

Solution
Run this command:

vagrant plugin install winrm-elevated

What Is The Etymology of the Terms Level-Trigger and Edge-Trigger?

Problem scenario
You have read about level-trigger and edge-trigger notification mechanisms in Linux. Why are they called level-trigger and edge-trigger? Where do they get their names?

Answer
Electronics and processing electric signals. Edges represent changes to a state. A level state represents the lack of a change. Here is a diagram:

This answer was adapted from the following postings:
https://hackernoon.com/photos/QdWX77c6s5Skk99abuNLlG6AEZZ2-9w23y8s
http://venkateshabbarapu.blogspot.com/2013/03/edge-triggered-vs-level-triggered.html

FFR
Notification mechanisms are components of input and output in Linux. There are two main types of notification mechanisms in this context.

A level-triggered notification is comes from a file descriptor that "is considered to be ready if it is possible to perform an I/O system call without blocking." (Taken from page 1329 of The Linux Programming Interface.)

An edge-triggered notification is a notification that "is provided if there is I/O activity (e.g., new input) on a file descriptor since it was last monitored." (Taken from page 1329 of The Linux Programming Interface.)

Level-triggered notifications are used by the select and poll system calls (according to page 1329 of The Linux Programming Interface). LT notifications are also used by the epoll "I/O event notification facility" (as taken from the man page for epoll).

Edge-tiggered notifications are used by signal-drive I/O and the epoll "I/O event notification facility" (as taken from the man page for epoll) in Linux (according to page 1329 of The Linux Programming Interface).

How Do You Write a “Hello World” Command in Rust?

Problem scenario
You want to write a basic program in Rust to know you have it installed correctly. What do you do?

Solution

  1. Install Rust. If you need assistance, try this command:
sudo curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

2. Create a file called contint.rs with the following four lines:

// We recommend calling this contint.rs
fn main() {
    println!("Hello World!");
}
  1. Compile it with this command: rustc contint.rs
  2. Run the bytecode by using this command: ./contint

What Does the “-> str” Syntax Signify in Python Functions?

Problem scenario
You see Python code with this "-> str" syntax. What does it do?

Answer
It is purely for readability. To help your coworkers we recommend using the "-> str" syntax if a function returns a string. Otherwise the way to find out is to read the code or temporarily change the code and use type(variable_returned) to show the data type of that which is/was returned.

Here is a program that illustrates how we know the "-> str" is just for information and has no affect on the code:

def documentation_proof() -> str:
  var1 = "unmistakeably a string"
  #have either the above or below var1 definition uncommented. have only one.  it proves the "-> str" syntax is for information only.
  #var1 = 55
  return var1

check_output = documentation_proof()
print(check_output)


Some functions will be configured as "-> None" if the function returns nothing. (If the function actually does return something, the "-> None" will have no effect other than misleading people when they read the function itself.

Similarly, the parameters in a function's definition can display intended data types like this:

def nifty_function(a: int, b: str):

But those parameter data type definitions do nothing special beyond enhancing readability. If you passed a non-integer for "a" or a non-string for "b", the function would proceed as if there was no ": int" or ": str" in the function parameters. Some programmers do not use this explicit convention as it is arguably more prolix and makes the code busy to read.

How Do You Choose a User ID or a Group ID in Linux?

Problem scenario
You are configuring UID and GID values in Linux. You want to have some idea of what values to use. What ranges should you target?

Solution
Use an integer between 3000 and 4999 (inclusive) if you have no other requirements. This range is recommended. To learn more about why this range is recommended, see this external posting.

In Object-Oriented Programming What Is an Object’s Operation’s Signature?

Question
You are learning about OOP. You know object's have operations. You read about operations having signatures. What are signatures?

Answer
An object's operation's signature is the composite of these three things:

  1. the operation's name
  2. the parameter objects the operation accepts
  3. the return value of the operation

(This was paraphrased from page 13 of Design Patterns). To learn more about what a signature is, see these links: