How Does sorted(list_arg1, key=arg2) Work in Python?

Question
You see that Python's sorted reserved word has an option to use a key (rather than the default mechanism). How does the key parameter/flag in Python's sorted function work?

Short Answer:
The word "key" designates a special function that you create. It accepts two parameters and returns positive or negative numbers. These help give you the ability to sort the list in a non-standard way.

Detailed Answer:
"The value (or values) that determines the sorting order is referred to as the key." (This quote was taken from page 144 of Programming Interviews Exposed.)

The function is invoked as a key with syntax like this:

sorted(contint_list, key=functools.cmp_to_key(cool_comp))

This function "cool_comp" is the arbitrary name and "cmp_to_key" is part of the "functools" library. (The above would need "import functools" in the Python program to work properly; it would also need "cool_comp" or whatever function name you choose to be defined). This "cool_comp" can return different values. However if it returns a True or False, the regular "sorted" feature will happen with default functionality (as if no key= was designated).

If the function in the key ("cool_comp" in this example) of sorted returns negative numbers (integers or decimals), the function will change the position of the two values in comparison.

In the sorted() keyword, if the function returns a negative value, the two values in the list will be swapped. If the function returns a positive value, the two values in the list will not be swapped.

The function via "key=functools.cmp_to_key(foobar)" syntax will be called with sending to parameters from the list. It will start with the leftmost two elements and iterate toward the right (taking two elements in the list at a time). The number of comparisons varies based on what is in the list and their current order. Some lists are almost already in order while others are in reverse order or random order.

The function "foobar" (or "cool_comp" in the example above) as an optional "key" function for the "sorted" feature should be defined to accept two parameters exactly.

Often sorting involves alphabetizing or ordering numbers in a sequence from small to large. However some sorting for complex use-cases may not be this trivial. Thus keys are involved for handling a composite number of smaller components/inputs for sophisticated sorting beyond ordering numbers or letters in default fashions. Incorporating a second component, such as a sequence of integers to the items in the array, can facilitate tracking whether the sorting algorithm is "stable." A sorting algorithm is said to be "stable" if it preserves the original order in the case of two equivalent keys. Some keys are not unique and their corresponding elements are how a person knows if the original order is preserved after the sorting of the array.

In some cases the key is very complex -- handling multiple data points with a formula that is based on a computation involving these different factors. With these types of keys, swapping the position of two elements in a list is a faster and less resource intensive operation than comparing the two elements. In these cases, selection sort may be an optimal/performant solution (per page 145 of Programming Interviews Exposed). We believe that implementing a sorting algorithm beyond the sorted reserved word and including a key, such as coding selection sort, would be rare in a business environment but possible in an interview.

Advertisement
If you want to purchase a book on Python, see this posting.

How are Provisioners in Terraform Different from User Data?

Question
How are Terraform provisioners different from Terraform User Data? Both usually involve scripts to customize a server.

Answer
Provisioners have built-in support for CM tools and can have scripts longer than 16 KB; these things are not true with User Data. (This was taken from page 213 of Terraform: Up & Running, 2nd Edition by Yevgeniy Brikman (O'Reily), Copyright 2019, 978-1-492-04690-5.) User Data is recommended unless you have more than 16 KB of scripts or if you want to deploy/leverage configuration management tools.

How Do You Sleep when It Is Light Out?

Problem scenario
You live in a northern latitude and it stays light until late at night and gets light out early in the morning. How do you sleep under these conditions?

Solution
Background information: Sleep is important. Research says you should try to get more than eight hours of sleep.

Buy a high quality sleep mask (as opposed to a very affordable one). There is a difference in the usefulness and comfort; if you have only used inexpensive ones, you may want to try a new one.

If you travel regularly, you may want to buy a travel pillow or an inflatable pillow.

If you want tips on how to deal with noise, see this article.

How Do You Find the Working Directory in an Azure DevOps Release Pipeline?

Problem scenario
A Terraform task is failing in an Azure DevOps release pipeline. You cannot see a variable setting for the working directory. How do you determine the working directory?

Solution
Go to Edit your release pipeline. Go to "View YAML" in the upper right hand corner. Search for "working". You should see it (e.g., as workingDirectory).

The working directory key probably has a value that is a variable (e.g., $foobar). Find $foobar in the relevant Task in the Azure DevOps Release pipeline (and remember that Terraform tasks have a Terraform logo near them.). Consider changing the $foobar variable (e.g., use an option to drill down). The variable key may be called (or invoked) in the "Configuration Directory" in the Azure DevOps pipeline task. The $foobar variable value may need to be changed (but these directions are just for identifying its value). You may need to look at the individual Tasks of the release or the Variables tab of the pipeline.


This solution was partially adapted from two independent articles (external blog posting here and another one here) and this medium.com article.

What Is The Disambiguation of The Word “heap” in I.T.?

Problem scenario
You have read about heaps in computer programming and in operating systems. What do the different definitions of "heap" mean in I.T.?

Overview
Merriam-Webster's Dictionary (11th Edition, on page 574) defines a heap as "a collection of things thrown one on another" or as a "pile." The same word can have completely different meanings in English; a trash heap, a heap in computer programming, a heap in the context of operating systems, and a heap in databases are a few types of "heaps". Their definitions are completely independent of one another.

Possible Solution #1 (for computer programming)
A heap is an abstract data structure that is a binary tree with the maximum value as the root. To learn more see https://www.educative.io/edpresso/what-is-a-heap

Cracking the Coding Interview says a heap is a complete binary tree that has nodes that are ordered (page 103). If a tree (a data structure) has the heap property it is said to be a heap.

Possible Solution #2 (for operating systems)
An allocation of memory. To learn more, see https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ The heap is a segment of a process's memory; it is "an area from which programs can dynamically allocate extra memory." (The source of this quote is page 31 of The Linux Programming Interface.)

To learn more about the disambiguation, see this link: https://techterms.com/definition/heap

Possible Solution #3 (for relational/SQL databases)
"A heap is a table without a clustered index." (Taken from Microsoft.)

"A heap-table stores the rows in an unordered fashion." (Taken from use-the-index-luke.com.)

AWS Quiz

1. Which is one of the five pillars of a well-architected AWS framework?

a. Archived versions (of IaC)
b. Disaster Recovery
c. Monitoring
d. Performance Efficiency
e. None of the above

2. Signature version 4 is which of the following?

a. A certificate file from Amazon Web Security.
b. An SSL public key for AWS.
c. An SSL private key for AWS.
d. It is the AWS HTTP authentication method that combines the access key ID and secret access key.

3. For dependencies of an AWS Lambda layer, how many layers are allowed?

a. 1
b. 2
c. 5
d. Unlimited

4. How many parameters can be passed to a CloudFormation template?

a. 3
b. 30
c. 60
d. 100
e. Unlimited

5. What is Fn in CloudFormation templates?

_____________________________

6. What percentage of professional hours of a migration project for an application into AWS is normally spent on testing?

a. 2%
b. 15%
c. 30%
d. 45%

7. Which of the six Rs (for an AWS Migration) is described as "drop-and-shop"?

a. rehost
b. replatform
c. repurchase
d. refactor/re-architect
e. retain
f. retire

8. Which of the six Rs (for an AWS Migration) is described as "lift-and-shift"?

a. rehost
b. replatform
c. repurchase
d. refactor/re-architect
e. retain
f. retire

9. Which three of the following are not part of the five phases of a migration that AWS recommends (per their five-phase migration process diagram)?

a. Migration Preparation & Business Planning
b. Quality Assurance
c. Portfolio Discovery & Planning
d. Administrate
e. Migration & Validation
f. Quality of service testing
g. Application Design
e. Operate

10. Which of the six Rs (for an AWS Migration) is described as "lift-tinker-and-shift"?

a. rehost
b. replatform
c. repurchase
d. refactor/re-architect
e. retain
f. retire

11. Is RDS an example of a serverless AWS offering?

Yes
No

12. Is Lambda an example of a serverless AWS offering?

Yes
No

13. An Amazon VPC can include which of the following?

a. VPNs
b. routing rules
c. subnets
d. NACLs (network ACLs)
e. All of the above
f. None of the above

14. The WorkSpaces "Running Mode" dramatically affects which of the following?

a. The processing performance (i.e., CPU) of the WorkSpace itself.
b. The memory performance (i.e., RAM) of the WorkSpace itself.
c. The duration the WorkSpace will be available to you.
d. None of the above.

15. What is a Change Set in CloudFormation?
___________________________________

16. What is the difference between a template and a stack in CloudFormation?

___________________________________________

17. VPCs are referred to as what in AWS when you are configuring an EC-2 instance?

a. Clusters
b. Sections
c. Networks
d. Subnets
e. Isolation chambers

18. What is a Lambda layer?

a. A line of code in a Lambda function
b. A Lambda used by another Lambda
c. A Lambda that creates one or more VPCs
d. None of the above.

For the answers, click here.

AWS Quiz Answers

1. Which is one of the five pillars of a well-architected AWS framework?

a. Archived versions (of IaC)
b. Disaster Recovery
c. Monitoring
d. Performance Efficiency
e. None of the above

Answer: D. Source: https://aws.amazon.com/blogs/apn/the-5-pillars-of-the-aws-well-architected-framework/

2. Signature version 4 is which of the following?

a. A certificate file from Amazon Web Security.
b. An SSL public key for AWS.
c. An SSL private key for AWS.
d. It is the AWS HTTP authentication method that combines the access key ID and secret access key.

Answer: D. Source:
https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html

3. For dependencies of an AWS Lambda layer, how many layers are allowed?

a. 1
b. 2
c. 5
d. Unlimited

Answer: C. Source https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

4. How many parameters can be passed to a CloudFormation template?

a. 3
b. 30
c. 60
d. 100
e. Unlimited

Answer: C. Source: https://aws.amazon.com/cloudformation/faqs/

5. What is Fn in CloudFormation templates?

Answer: Fn is the syntax to refer to different intrinsic functions. These functions do various things such as return values during CloudFormation operations. There is a cfn-init command that is part of AWS CloudFormation. Apparently the cfn stands for CloudFormation. To learn more, click here.

6. What percentage of professional hours of a migration project for an application into AWS is normally spent on testing?

a. 2%
b. 15%
c. 30%
d. 45%

Answer: D. Source page 7 of https://d0.awsstatic.com/whitepapers/Migration/migrating-applications-to-aws.pdf

7. Which of the six Rs (for an AWS Migration) is described as "drop-and-shop"?

a. rehost
b. replatform
c. repurchase
d. refactor/re-architect
e. retain
f. retire

Answer: C. Source: Page 13 of https://d1.awsstatic.com/Migration/migrating-to-aws-ebook.pdf

8. Which of the six Rs (for an AWS Migration) is described as "lift-and-shift"?

a. rehost
b. replatform
c. repurchase
d. refactor/re-architect
e. retain
f. retire

Answer: A. Source: Page 13 of https://d1.awsstatic.com/Migration/migrating-to-aws-ebook.pdf

9. Which three of the following are not part of the five phases of a migration that AWS recommends (per their five-phase migration process diagram)?

a. Migration Preparation & Business Planning
b. Quality Assurance
c. Portfolio Discovery & Planning
d. Administrate
e. Migration & Validation
f. Quality of service testing
g. Application Design
e. Operate

Answer: B, D, F. Source page 8 of https://d1.awsstatic.com/Migration/migrating-to-aws-ebook.pdf

10. Which of the six Rs (for an AWS Migration) is described as "lift-tinker-and-shift"?

a. rehost
b. replatform
c. repurchase
d. refactor/re-architect
e. retain
f. retire

Answer: B. Source: Page 13 of https://d1.awsstatic.com/Migration/migrating-to-aws-ebook.pdf

11. Is RDS an example of a serverless AWS offering?

Yes
No

Answer: No. See https://acloud.guru/forums/aws-csa-2019/discussion/-LtPWTfYtKgh4qX97LlO/RDS%20is%20NOT%20Serverless

12. Is Lambda an example of a serverless AWS offering?

Yes
No

Answer: Yes. See https://aws.amazon.com/lambda/

13. An Amazon VPC can include which of the following?

a. VPNs
b. routing rules
c. subnets
d. NACLs (network ACLs)
e. All of the above
f. None of the above

Answer: E. Source: Page 94 of Terraform Up & Running.

14. The WorkSpaces "Running Mode" dramatically affects which of the following?

a. The processing performance (i.e., CPU) of the WorkSpace itself.
b. The memory performance (i.e., RAM) of the WorkSpace itself.
c. The duration the WorkSpace will be available to you.
d. None of the above.

Answer: C. Source: https://aws.amazon.com/workspaces/pricing/
You must buy an entire month of the WorkSpace if you configure the WorkSpace's "Running Mode" as an "AlwaysOn". Early cancellations will not allow you to save money if you use it for even one minute.

15. What is a Change Set in CloudFormation?
___________________________________

Answer: It is the difference between a deployed stack and a new desired state for the stack. Change sets allow you to preview the changes to the stack. They can be a dry run or something to be executed.
Source: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets.html

16. What is the difference between a template and a stack in CloudFormation?

___________________________________________

Answer: A stack is a deployed set of IaaS and/or PaaS components in AWS that usually is configured to interact with itself in a useful way. A template is a JSON or YAML file used to deploy a stack.
Source: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-whatis-concepts.html

17. VPCs are referred to as what in AWS when you are configuring an EC-2 instance?

a. Clusters
b. Sections
c. Networks
d. Subnets
e. Isolation chambers

Answer: C. When you configure a new EC-2 instance, the Step 3's title is "Configure Instance Details". There is a "Network" drop down menu next to a link "Create new VPC."

18. What is a Lambda layer?

a. A line of code in a Lambda function
b. A Lambda used by another Lambda
c. A Lambda that creates one or more VPCs
d. None of the above.

Answer: D. Source:
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

How Do You Boot to One of Two Hard Drives?

Problem scenario
You have two hard drives in your laptop. You want to be able to boot to one or the other. The BIOS options are confusing. What do you do?

Possible Solution (directions primarily for HP laptops)

  1. Power off the laptop.
  2. Turn on the laptop and press "Esc" for the Start Menu.
  3. Go to F10 BIOS Setup.
  4. Go to the "Security" tab.
  5. Go to the Boot Options -> Boot Mode. Choose either Legacy or "UEFI Native (without CSM)". If you alternate between these you may be able to boot to the hard drive of your choice.
  6. In the Advanced tab, go to Multiboot Express Popup Delay (sec) -> Choose 5 sec (or longer).
  7. Save all the changes. Each time you switch between them, you may have to go into this Startup Menu and the Security Tab and perform step #5 again. We know this is not an optimal solution, but it may or may not help you.