An alternative algorithm for sorting a stack using a single extra stack

From "Cracking the Coding Interview":

Write a program to sort a stack in ascending order (with the largest items at the top). You can use up to one additional stack to hold items, but you can not copy items to another data structure (such as an array). The battery supports push, pop, is_empty, and peek

So, the classic solution I found online for this (and the one in the book) looks like this:

Algo # 1 (classic)

def sort_stack(primary):
  secondary = ()
  while primary:
    tmp = primary.pop()
    while (secondary and secondary(-1) > tmp):
  return secondary

In summary, we will return our secondary / auxiliary battery after sorting via time O (n ^ 2).

However, this is not what my original approach was and I think this approach has some interesting qualities:

Algo # 2 (mine)

def sort_stack(primary):
  did_sort = False
  secondary = ()
  while not did_sort:
    # move from primary to secondary, pushing larger elements first when possible
    desc_swap(primary, secondary)
    # move from secondary back to primary, pushing smaller elements first when possible. Set did_sort = True if we're done and can exit.
    did_sort = asc_swap(secondary, primary)
  return primary

def asc_swap(full, empty):
  temp = None
  did_sort = True
  yet_max = None

  while full:
    if not temp:
      temp = full.pop()
    if full:
      if full(-1) < temp:
        insert = full.pop()
        if insert < yet_max:
          did_sort = False
        yet_max = insert
        temp = None
  if temp:
  return did_sort

def desc_swap(full, empty):
  temp = None
  while full:
    if not temp:
      temp = full.pop()
    if full:
      if full(-1) > temp:
        temp = None
  if temp:

Now, obviously, it is not as clean or elegant, but it could be with some help functions that dynamically choose our comparator and choose the element to push, etc.

Basically, what he does is this:

# Start with stack in wrong order (worst case)
primary:   4 3 2 1

# Swap to secondary, pushing larger elements first (1 is held in temp until the end because it is smaller than the following elements)
secondary: 2 3 4 1

# Swap back to primary, pushing smaller elements first
primary:   1 3 2 4

# back to secondary 
secondary: 4 3 2 1

# Back to primary, finished
primary:   1 2 3 4

This strategy has a compromise between the best and the worst case.
Algo # 1 actually performs worst when the stack is already sorted and better when sorted in the wrong order and algo # 2 does the opposite.


  • What are your thoughts? I think it's just an interesting way to sort out what I've never seen before.
  • Is there a name for this kind of sorting? I could not find similar algos but I am sure they are there and would like to be able to describe / recognize it better. Thank you!

BitMEX CEO Expects Bitcoin at 20,000 USD! Enter the free Bitmex signal of the day to make a 150% profit on a single exchange. – Advertising, offers

Visit them to make huge profits, free daily crypto signal and help with Bitmex Leverage Trade –

Bitcoin could reach $ 20,000 as a result of emergency measures taken by the US Federal Reserve. Arthur Hayes, CEO of BitMEX, predicted that the new Quantitative Easing (QE) would further reduce confidence in the fiat currency.

mUSE4BJ.jpg? 1
8rXqT9g.jpg? 1

13% big profits thanks to an excellent scalping trade generated via Bot. It makes a continuous profit without any manual intervention.

Discover this community, they have an incredible TRX strategy. 11% profit on #TRX generated automatically via Bot. Their accuracy was excellent on #TRX Trades.

Just set the Bot and get all #TRX Scalper Translators. Bot detects the trend and performs automatic transactions that allow you to get offline time.

. (tagsToTranslate) crypto trading (t) bitmex (t) binance

Has Canada transferred a single entry into multiple entry visas?

Anyone applying for a visitor visa in Canada is automatically considered for a multiple entry visa. You do not need to specify it in your application.

The official website of Immigration Canada explains:

You do not need to choose. A multiple entry visa is what all visa applicants are automatically considered. We will review your application and issue a visa depending on your situation.

Multiple entry visa

As long as it is valid, a multiple entry visa will allow you to travel to Canada for six months at a time and as many times as you wish. It will be valid until 10 years or a month before the expiry of your passport, whichever is shorter. You must arrive in Canada no later than the date your visa expires.

Single entry visa

A single entry visa allows you to travel to Canada once. For example, you can only be eligible for a single entry visa if:

  • you are eligible for a fee waiver and the purpose of your entry into Canada is limited (for example, for an official visit by a foreign national)
  • you are taking part in a unique special event in Canada
  • there are approved procedures or guidelines specific to each country.

In most cases, once you have left Canada, you will need a new visa to re-enter Canada.

You do not need a new visitor visa to return to Canada if you go directly to the United States (including its territories and possessions) or Saint-Pierre-et-Miquelon.

json rpc – Use RPC Bitcoin to send multiple addresses to a single address

Yes that possible:

you need to know the txid and Vout indexes for these particular addresses when they were funded

for example here, I used 3 recipients to send BTCs to unique addresses 2N6V6Ko17CPWaZZf6YrGKeq8R1fzbqrTKLF and if i know the txid and name of these addresses when they were funded, i will do:

1] create a gross transaction

syntax: createrawtransaction   [{"txid":txid,"vout":n},...] {address:amount,...} 

 bitcoin-cli -regtest createrawtransaction '[{"txid":"582283c5f2293a2cdb27173c3118ff59863d9ff2be5c3b48fa8e66509e9a7d0d","vout":1},{"txid":"3126fc2864fbf780460734dd1512b98e86f72f8e5b8b59047beae9dd859ac33a","vout":0},{"txid":"94a6701b68e38b3f980c9bf2b0e905dd7e479c7ed01590eeee20b1753c29d6af","vout":1}]' '{"2N6V6Ko17CPWaZZf6YrGKeq8R1fzbqrTKLF":"2"}'

2] Sign the gross transaction:

syntax: signrawtransaction   [] [,...] 

bitcoin-cli -regtest  Signrawtransaction hexstring_from_createRawtransaction '[]'["pvtkey_add1","pvtkey_add2","pvtkey_add3"]

3] send a gross transaction

syntax: sendrawtransaction hexString

bitcoin-cli -regtest hexString_from_signrawTransction

linux – Multiple GlusterFS bricks replicated on a single node

The current situation is that I have a 4x2tb (1 brick) raid, which I will save on an external USB enclosure (1 brick). I hope that GlusterFS will allow me to replicate the main brick of my raid on my external USB brick located on the same node. Over time, I plan to migrate him to a Raspi and move him, but I want to familiarize myself with his functioning and have the basic work already operational. The brilliance looks excellent and I hope it will be possible even though all the places I visited gave the impression that there had to be several knots.

Aggelematic geometry – Endomorphism ring classification of single abelian patterns greater than $ W (k) / p ^ n $

Yes $ A $ is a simple abelian variety on a closed field algebraically k $ with then $ End (A_k) otimes { mathbb Q} $ is a positive involution divisional algebra. Albert's classification tells us that these types of divisional algebras fall into four types.

Now consider an algebraically closed field k $ of characteristic $ p> 0 $ and a positive integer $ n $is there an (approximate) classification of $ End ( mathcal {A}) $ for simple abelian schemas $ mathcal {A} $ more than $ W (k) / p ^ n $ (here simplicity means $ mathcal {A} _k $ Is simple) ?

If we have an abelian schema $ A $ more than $ W (k) $, can we determine the growth of $ End (A text {mod} p ^ n) $ ?

Motivation: Gross's theory of canonical lift and quasi-canonical emergence for elliptic curves, Grothendieck-Messing theory.

Testing the multiplayer browser game on a single computer

I write a game based on a multiplayer browser and I encounter a simple interaction problem with the game test. When a browser window loses focus, the execution of requestAnimationFrame calls ceases . This effectively pauses all visuals when focus is lost.

I would like to have two instances of the game running at the same time. I could do a test test in a window and observe how a third party would see these actions in another window.

At the present time, I can only switch between the two windows and see the behavior of the teleportation when the client's state catches the new server state.

I do not know how, without setting up a second computer, I could test that. Does anyone have an experience with this or a council?

8 – Rendering a multipage form from a single function

Is it possible to create a multipage form from a single function?

I watched the MultistepForm.php script in the example module that uses a separate function to render each page of the example form in several steps.

My challenge is that I do not know in advance how many steps my form will take. The entire form is predefined, serialized and saved in the database.

At the moment I retrieve and deserialize the form, I try to place each item on an individual page. My current buildForm code follows:

public function buildForm(array $form, FormStateInterface $form_state){
            $form = array();

            $nid = Drupal::routeMatch()->getParameter('node');
            $questions = QueryGenerator::fetchQuestions($nid);
            // dsm($questions, 'QUESTIONS');

            foreach($questions as $question){
              array_push($form, unserialize($question->processed));

            $form('separator') = (
              '#markup' => '
', ); $form('submit') = ( '#type' => 'submit', '#value' => $this->t('Submit'), ); return $form; }

Can any one tell me how I could accomplish that, please?

sql – Associate multiple ID numbers to a single product in Access

I have a database with the following structure:

ID Product Name
1 product A
2 product B
3 product C
4 product A
T5 Product A

Some products have multiple numeric and alphanumeric identifiers. If a product has only a numeric string and an alphanumeric string, I have solved the problem. However, some products contain a few dozen.

My final goal is to produce a report as follows

Product name ID
Product A 1 4 T5
Product B 2
Product C 3

google sheets – How to apply two conditional formatting rules to a single cell

I have two groups of rules … a color code filling cells based on a condition of visual grouping of data (7 rules for 7 groups); the other color codes the text of the cells according to another condition for an alert status.

I set the text color to "none" in the fill rule and the fill color to "none" in the text ruler, but it appears that the default colors are applied by the priority rule. higher and the lowest is ignored.

Is it possible to superimpose rules on multiple conditions to achieve this effect without having to enumerate an absurd amount (x * y) of rule combinations?