Infinite looping Issue Python. Can't quit game

Question: I made a code for Blackjack in Python and whenever I run blackjack_game(deck) saying no to the 'Play Again' input should quit the game but it doesn't. Funds going zero and below should also trigger the game to quit but it doesn't.
This is what it looks like:
import random
import os

# The Card class definition
class Card:
    def __init__(self, suit, value, card_value):

        # Suit of the Card like Spades and Clubs
        self.suit = suit

        # Representing Value of the Card like A for Ace, K for King
        self.value = value

        # Score Value for the Card like 10 for King
        self.card_value = card_value

# Clear the terminal
def clear():
    os.system("clear")

# Print player stats
def print_stats(player_name, funds, wins, losses, ties, blackjacks, busts):
    print('Player: ', player_name)
    print('Funds: $', funds)
    print(f'Wins: {wins} Losses: {losses} Ties: {ties} Blackjacks: {blackjacks} Busts: {busts}')

# Function to print the cards
def print_cards(cards, hidden):

    s = ""
    for card in cards:
        s = s + "\t ________________"
    if hidden:
        s += "\t ________________"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"
    print(s)

    s = ""
    for card in cards:
        if card.value == '10':
            s = s + "\t|  {}            |".format(card.value)
        else:
            s = s + "\t|  {}             |".format(card.value)
    if hidden:
        s += "\t|                |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|      * *       |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|    *     *     |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|   *       *    |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|   *       *    |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|       {}        |".format(card.suit)
    if hidden:
        s += "\t|          *     |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|         *      |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|        *       |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|                |"
    if hidden:
        s += "\t|                |"
    print(s)

    s = ""
    for card in cards:
        if card.value == '10':
            s = s + "\t|            {}  |".format(card.value)
        else:
            s = s + "\t|            {}   |".format(card.value)
    if hidden:
        s += "\t|        *       |"
    print(s)

    s = ""
    for card in cards:
        s = s + "\t|________________|"
    if hidden:
        s += "\t|________________|"
    print(s)

    print()

# Function for a game of blackjack
def blackjack_game(deck):
    end_game = False
    play_again = 'Y'

    # Player name
    player_name = str(input('Enter player name: '))

    # Intro
    print('Lets have a fun game of Blackjack, ', player_name)

    # Cards for both dealer and player
    player_cards = []
    dealer_cards = []

    # Scores for both dealer and player
    player_score = 0
    dealer_score = 0

    # Player stats
    funds = 100
    wins = 0
    losses = 0
    ties = 0
    blackjacks = 0
    busts = 0

    bet = 0

    clear()

    # Current Stats Display
    print_stats(player_name, funds, wins, losses, ties, blackjacks, busts)

    # Bets
    while play_again == 'Y':
     while end_game == False:
      while funds > 0:
       while bet == 0:
        bet = int(input('Enter bet amount: '))

        if bet > funds:
         print('Insufficient funds')
         bet = 0

       # Initial dealing for player and dealer
       while len(player_cards) < 2:

        # Randomly dealing a card
        player_card = random.choice(deck)
        player_cards.append(player_card)
        deck.remove(player_card)

        # Updating the player score
        player_score += player_card.card_value

        # In case both the cards are Ace, make the first ace value as 1
        if len(player_cards) == 2:
            if player_cards[0].card_value == 11 and player_cards[1].card_value == 11:
                player_cards[0].card_value = 1
                player_score -= 10

        # Print player cards and score
        print("PLAYER CARDS: ")
        print_cards(player_cards, False)
        print("PLAYER SCORE = ", player_score)

        input()

        # Randomly dealing a card
        dealer_card = random.choice(deck)
        dealer_cards.append(dealer_card)
        deck.remove(dealer_card)

        # Updating the dealer score
        dealer_score += dealer_card.card_value

        # Print dealer cards and score, keeping in mind to hide the second card and score
        print("DEALER CARDS: ")
        if len(dealer_cards) == 1:
            print_cards(dealer_cards, False)
            print("DEALER SCORE = ", dealer_score)
        else:
            print_cards(dealer_cards[:-1], True)
            print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)

        # In case both the cards are Ace, make the second ace value as 1
        if len(dealer_cards) == 2:
            if dealer_cards[0].card_value == 11 and dealer_cards[1].card_value == 11:
                dealer_cards[1].card_value = 1
                dealer_score -= 10

        input()

       clear()

       # Print dealer and player cards
       print("DEALER CARDS: ")
       print_cards(dealer_cards[:-1], True)
       print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)

       print()

       print("PLAYER CARDS: ")
       print_cards(player_cards, False)
       print("PLAYER SCORE = ", player_score)

       # Managing the player moves
       while player_score < 21:
        choice = input("Enter H to Hit or S to Stand : ")

        # Sanity checks for player's choice
        if len(choice) != 1 or (choice.upper() != 'H' and choice.upper() != 'S'):
            clear()
            print("Wrong choice!! Try Again")

        # If player decides to HIT
        if choice.upper() == 'H':

            # Dealing a new card
            player_card = random.choice(deck)
            player_cards.append(player_card)
            deck.remove(player_card)

            # Updating player score
            player_score += player_card.card_value

            # Updating player score in case player's card have ace in them
            c = 0
            while player_score > 21 and c < len(player_cards):
                if player_cards[c].card_value == 11:
                    player_cards[c].card_value = 1
                    player_score -= 10
                    c += 1
                else:
                    c += 1

            clear()

            # Print player and dealer cards
            print("DEALER CARDS: ")
            print_cards(dealer_cards[:-1], True)
            print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)

            print()

            print("PLAYER CARDS: ")
            print_cards(player_cards, False)
            print("PLAYER SCORE = ", player_score)

        # If player decides to Stand
        if choice.upper() == 'S':
            break

       clear()

       # Print player and dealer cards
       print("PLAYER CARDS: ")
       print_cards(player_cards, False)
       print("PLAYER SCORE = ", player_score)

       print()
       print("DEALER IS REVEALING THE CARDS....")

       print("DEALER CARDS: ")
       print_cards(dealer_cards, False)
       print("DEALER SCORE = ", dealer_score)

       # Check if player has a Blackjack
       if player_score == 21:
        print("PLAYER HAS A BLACKJACK")
        blackjacks += 1

       # Check if player busts
       if player_score > 21:
        print("PLAYER BUSTED!!!")
        busts += 1
        print("DEALER WINS!!!")
        losses += 1
        funds -= bet
        bet = 0
        print_stats(player_name, funds, wins, losses, ties, blackjacks, busts)
        end_choice = input('Play again(Y/N)?: ')
        play_again = end_choice.upper()
        player_cards = []
        dealer_cards = []
        player_score = 0
        dealer_score = 0
        end_game = True

       input()

       # Managing the dealer moves
       while dealer_score < 17:
        clear()

        print("DEALER DECIDES TO HIT.....")

        # Dealing card for dealer
        dealer_card = random.choice(deck)
        dealer_cards.append(dealer_card)
        deck.remove(dealer_card)

        # Updating the dealer's score
        dealer_score += dealer_card.card_value

        # Updating player score in case player's card have ace in them
        c = 0
        while dealer_score > 21 and c < len(dealer_cards):
            if dealer_cards[c].card_value == 11:
                dealer_cards[c].card_value = 1
                dealer_score -= 10
                c += 1
            else:
                c += 1

        # print player and dealer cards
        print("PLAYER CARDS: ")
        print_cards(player_cards, False)
        print("PLAYER SCORE = ", player_score)

        print()

        print("DEALER CARDS: ")
        print_cards(dealer_cards, False)
        print("DEALER SCORE = ", dealer_score)

        input()

       # TIE Game
       if dealer_score == player_score:
        print("TIE GAME!!!!")
        ties += 1
        bet = 0
        print_stats(player_name, funds, wins, losses, ties, blackjacks, busts)
        end_choice = input('Play again(Y/N)?: ')
        play_again = end_choice.upper()
        player_cards = []
        dealer_cards = []
        player_score = 0
        dealer_score = 0
        end_game = True

       # Dealer busts
       elif dealer_score > 21:
        print("DEALER BUSTED!!! YOU WIN!!!")
        wins += 1
        funds += bet
        bet = 0
        print_stats(player_name, funds, wins, losses, ties, blackjacks, busts)
        end_choice = input('Play again(Y/N)?: ')
        play_again = end_choice.upper()
        player_cards = []
        dealer_cards = []
        player_score = 0
        dealer_score = 0
        end_game = True

       # Dealer gets a blackjack
       elif dealer_score == 21:
        print("DEALER HAS A BLACKJACK!!! PLAYER LOSES")
        losses += 1
        funds -= bet
        bet = 0
        print_stats(player_name, funds, wins, losses, ties, blackjacks, busts)
        end_choice = input('Play again(Y/N)?: ')
        play_again = end_choice.upper()
        player_cards = []
        dealer_cards = []
        player_score = 0
        dealer_score = 0
        end_game = True

       # Player Wins
       elif player_score < 21 and player_score > dealer_score:
        print("PLAYER WINS!!!")
        wins += 1
        funds += bet
        bet = 0
        print_stats(player_name, funds, wins, losses, ties, blackjacks, busts)
        end_choice = input('Play again(Y/N)?: ')
        play_again = end_choice.upper()
        player_cards = []
        dealer_cards = []
        player_score = 0
        dealer_score = 0
        end_game = True

       # Dealer Wins
       else:
        print("DEALER WINS!!!")
        losses += 1
        funds -= bet
        bet = 0
        print_stats(player_name, funds, wins, losses, ties, blackjacks, busts)
        end_choice = input('Play again(Y/N)?: ')
        play_again = end_choice.upper()
        player_cards = []
        dealer_cards = []
        player_score = 0
        dealer_score = 0
        end_game = True

    quit()

if __name__ == '__main__':

    # The type of suit
    suits = ["Spades", "Hearts", "Clubs", "Diamonds"]

    # The suit value
    suits_values = {"Spades":"\u2664", "Hearts":"\u2661", "Clubs": "\u2667", "Diamonds": "\u2662"}

    # The type of card
    cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

    # The card value
    cards_values = {"A": 11, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":10, "Q":10, "K":10}

    # The deck of cards
    deck = []

    # Loop for every type of suit
    for suit in suits:

        # Loop for every type of card in a suit
        for card in cards:

            # Adding card to the deck
            deck.append(Card(suits_values[suit], card, cards_values[card]))

I added a quit() that should trigger should 'while play_again == 'Y':' no longer be true. This should have quit the game and stopped it from running. Instead it prompts the user again for a betting amount, acting as if I chose 'Y' instead.
I also tried removing:
play_again = 'Y'

and replacing this code block:
end_choice = input('Play again(Y/N)?: ')
play_again = end_choice.upper()

with this:
end_choice = input('Play again(Y/N)?: ')
if end_choice.upper() == 'N':
 exit()

But it still wouldn't quit and stayed as an infinite loop. Help me please, I've been stuck with this issue all day.
Comment: 400+ lines of code... I ran it, and nothing happened. If you hope to get a useful answer, reduce the code to 50 lines, removing anything that is not contributing to the problem you have. This code-trimming exercise might even lead you to a solution.
Answer: Can you please try by replacing from this:
    # Bets
    while play_again == 'Y':
        while end_game == False:
            while funds > 0:
                while bet == 0:
                    bet = int(input('Enter bet amount: '))

                    if bet > funds:
                        print('Insufficient funds')
                        bet = 0

With
    # Bets
    while play_again == 'Y':
        while end_game == False:
            if play_again != 'Y':
                break
            while funds > 0:
                if play_again != 'Y' or end_game:
                    break
                while bet == 0:
                    bet = int(input('Enter bet amount: '))

                    if bet > funds:
                        print('Insufficient funds')
                        bet = 0

I believe we don't even need to add end_game as it is unnecessary in current scope.
These are nested while loop and parent while loop will not break until child breaks. so we need to add extra condition in child while loop which break it on the basis of parent check.
you can read this post 5 Ways To Break Out of Nested Loops in Python
Answer: So the issue (to Usman's point) really comes from the nesting levels. Basically for the below logic, it will only check if play_again is still True once end_game is False, which it will only check once funds is no longer > 0.
while play_again == 'Y':
  while end_game == False:
    while funds > 0:
      while bet == 0:
        ...
      play_again = input("Play again?")

Your code would actually work fine if you had the proper indentation (end of loop being within the "play_again" or "end_game" levels), but unless there's a really good reason, it's typically better to combine conditionals rather than nesting them in multiple while loops that are easy to lose track of. Consider instead:
while (play_again == "Y") and (funds > 0):
   if bet == 0:
     bet = int(input('Enter bet amount: '))
   if bet < self.funds:
     print("Insufficient funds")
   ...

Other Notes
Based on the coding style it seems like you're at the start of your wonderful journey into Python, and I wanted to pay forward the help I got starting out by giving some (hopefully) useful tips in case this wasn't part of a structured course. I wound up slapping together an MVP for how this might look below (can build further), please read through the notes though additionally:

Break apart large functions! It'll make your life a lot easier, this looks to be maybe an initial project and you get a better knack for where to break things up as you move along, but a single function being ~300 lines is just going to make you miserable. General rule of thumb to have maybe 40-50 lines max unless there's a really good reason.
Utilize classes more (especially if you're trying to be OOP!), a "BlackjackGame" class could help keep methods and attributes organized vs having to set them only once per iteration, and helps keep track of variables across games (i.e. total value).
Type hinting to make your life easier. Depending on where you are in your learning path this might be a bit later on, but it makes things infinitely easier for working with Intellisense hints (see below using Pylance in VSCode). You could also just create a Deck class itself.

Sample
import itertools
import random
import os
from typing import List, Literal

def clear():
    os.system("clear")

class BlackjackRound:
    def __init__(self, deck):
        self.player_hand: Hand = Hand()
        self.dealer_hand: Hand = Hand()
        self.deck: List[Card] = deck

        ## Draw top cards
        for hand in [self.player_hand, self.dealer_hand]:
            for _ in range(2):
                self.deck = hand.draw(self.deck)

        self._print_stats()
        while self.player_hand.value < 21:
            choice = input("Enter H to Hit or S to Stand : ")
            if len(choice) != 1 or (choice.upper() != 'H' and choice.upper() != 'S'):
                clear()
                print("Wrong choice!! Try Again")
                continue
            if choice.upper() == 'H':
                self.deck = self.player_hand.draw(self.deck)
            if choice.upper() == 'S':
                break
            self._print_stats()
        ## Resolve Dealer Hand
        self._print_stats(mask_dealer=False)
        while self.dealer_hand.value < 17:
            print("Dealer draws...")
            self.deck = self.dealer_hand.draw(self.deck)
            self._print_stats(mask_dealer=False)
            ##For dramatic tension
            if self.dealer_hand.value < 17:
                input("Enter to continue...")

    def _print_stats(self, mask_dealer=True):
        print("You're hand")
        self.player_hand.print_cards()
        print(f"PLAYER SCORE = {self.player_hand.value}")

        print("Dealer showing...")
        if mask_dealer:
            self.dealer_hand.print_cards(hidden=True, n_cards=1)
            print(f"DEALER SCORE = {self.dealer_hand.cards[0].card_value}")
        else:
            self.dealer_hand.print_cards()
            print(f"DEALER SCORE = {self.dealer_hand.value}")

# The Card class definition
class Card:
    def __init__(self, suit, value, card_value, suit_format):
        # Suit of the Card like Spades and Clubs
        self.suit = suit
        # Representing Value of the Card like A for Ace, K for King
        self.value = value
        # Score Value for the Card like 10 for King
        self.card_value = card_value
        self.suit_format = suit_format
    #How should Card be printed?
    def __repr__(self):
        return str(self.__dict__)

class Hand:
    def __init__(self):
        self.cards: List[Card] = []
        self.value = 0

    def draw(self, deck: List[Card]):
        player_card = random.choice(deck)
        self.cards.append(player_card)
        deck.remove(player_card)
        self.value += player_card.card_value
        self.revalue_ace()
        return deck

    def revalue_ace(self):
        ##Sum greater than 21,
        if (self.value > 21) and ("A" in [card.value for card in self.cards]):
            swap_card = list(filter(lambda x: x.value == "A", self.cards))[0]
            self.cards[self.cards.index(swap_card)].card_value = 1
            self.value-=10
        else:
            pass

    def print_cards(self, hidden: bool=False, n_cards=None):
        s = ""
        cards = self.cards if n_cards is None else self.cards[0:n_cards]
        for card in cards:
            s = s + "\t ________________"
        if hidden:
            s += "\t ________________"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|                |"
        print(s)

        s = ""
        for card in cards:
            if card.value == '10':
                s = s + "\t|  {}            |".format(card.value)
            else:
                s = s + "\t|  {}             |".format(card.value)
        if hidden:
            s += "\t|                |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|      * *       |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|    *     *     |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|   *       *    |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|   *       *    |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|       {}        |".format(card.suit_format)
        if hidden:
            s += "\t|          *     |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|         *      |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|        *       |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|                |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|                |"
        if hidden:
            s += "\t|                |"
        print(s)

        s = ""
        for card in cards:
            if card.value == '10':
                s = s + "\t|            {}  |".format(card.value)
            else:
                s = s + "\t|            {}   |".format(card.value)
        if hidden:
            s += "\t|        *       |"
        print(s)

        s = ""
        for card in cards:
            s = s + "\t|________________|"
        if hidden:
            s += "\t|________________|"
        print(s)

        print()

class BlackjackGame:
    def __init__(self):
        ##play_again can only be Y/N
        self.play_again: Literal["Y", "N"] = "Y"
        self.player_name: str = input("Player name?: ")
        self.funds = 100
        self.wins = 0
        self.losses = 0
        self.ties = 0
        self.blackjacks = 0
        self.busts = 0

    def _print_stats(self):
        print(f"Current funds: {self.funds}")
    def play_game(self):
        self._print_stats
        self.shuffle_deck()
        bet = 0
        while (self.play_again == "Y") and (self.funds > 0):
            if bet == 0:
                bet = int(input('Enter bet amount: '))
            if bet < self.funds:
                print("Insufficient funds")
                continue #Restart at the top of the key
            round = BlackjackRound(self.deck)
            if round.player_hand.value == 21:
                print("Blackjack!")
                self.blackjacks+=1
                self.funds+=bet
            elif round.player_hand.value > 21:
                print("BUST")
                self.busts+=1
                self.funds-=bet
            elif round.dealer_hand.value > 21:
                print("DEALER BUST!")
                self.wins +=1
                self.funds+=bet
            elif round.player_hand.value > round.dealer_hand.value:
                print("Win!")
                self.wins+=1
                self.funds+=bet
            elif round.player_hand.value < round.dealer_hand.value:
                print("Lose!")
                self.losses+=1
                self.funds-=bet
            else:
                print("Tie!")
                self.ties+=1
                    ## Error handling for exit
                bet=0
            ##Update deck from round
            self.deck = round.deck
            while True:
                self.play_again = input('Play again(Y/N)?: ')
                if self.play_again == "N":
                    print("So long!")
                    quit()
                elif self.funds == 0:
                    print("Too bad, you're broke!")
                    quit()
                elif self.play_again == "Y":
                    print("Around we go!")
                print("Try again plz")

    def shuffle_deck(self,
        suits = {"Spades":"\u2664", "Hearts":"\u2661", "Clubs": "\u2667", "Diamonds": "\u2662"},
        cards = {"A": 11, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":10, "Q":10, "K":10}
        ):
        """
        Create a deck of 52 cards
        """
        self.deck = [Card(combo[0], combo[1], cards[combo[1]], suits[combo[0]]) for combo in itertools.product(suits.keys(), cards.keys())]

if __name__ == '__main__':
    bg = BlackjackGame()
    bg.play_game()


Sharps used in Super Mario theme

Question: Why are there sharps/flats in super Mario theme played on piano as the song is written in C major key.
I was reading this sheet music. I have seen many others and they all use sharps but write C major as key.
Aren't the chords used in chord progression part of C major scale, if so there shouldn't be any sharps.
Comment: What makes you believe that a composition in C major must only use notes from the C major scale?
Comment: @haxor789  It's *usually* better to spell a chromatic descent with flats, and a chromatic ascent the sharps.
Comment: @phoog I think the reason that the transcriber chose to spell it as an F# rather than as a Gb there, is because there is a G (natural) in the bass at that moment.
Comment: Does this answer your question? [How to make songs with chords that don't belong in a scale?](https://music.stackexchange.com/questions/12623/how-to-make-songs-with-chords-that-dont-belong-in-a-scale)
Comment: That F sharp in measures 7 and 9 and so on ought to be a G flat.  The accidentals in this video appear to be much better (though I didn't look very closely, so there could be some errors I missed): https://youtu.be/SvHf8jbmAUY
Comment: @haxor789: In many cases, there will be some advantages to using a flatted upper note, and some advantages to using a sharped lower note.  If one used a flatted upper note here, then after the G, there would be a Gb which would be in the same staff position, but have a flat in front of it to show the descent.  That would be followed by an F which, being at a lower spot on the staff, would also visibly descend and, having no sharp or flat, would be recognizable as a key note.  The tune as written makes it less visually clear that the G# is descending to the key note.
Comment: Those sharps and flats are called 'accidentals'.  They make it more interesting - less monochromatic.
Comment: @user1079505 I thought there might be some formula for music to make things easier.
Comment: Yet another question presuming diatonic notes only can be used in a composition! Who is spreading that 'theory'..?!! Could equally have been flats, too.
Comment: @phoog Could you explain why it would need to be a G flat rather than an F Sharp?
Comment: Ok that makes sense! Thanks Elements in Space and supercat. So it's essentially just a convention that neatly gets rid of this sharp followed by natural combination and instead only features one kind of accidental.
Comment: Let's say you're giving a speech about a certain topic. It's quite basic, and has been explained or researched by thousands before you. Sure, you can keep your speech just within that topic and still work it to make it "interesting". But what if you go just a bit off? You add some apparently unrelated arguments, that you're able to relate to what you're talking about. Then you add something that seems definitely off, but then you place that "new" in a different perspective, making both the new topic and the old quite more related and interesting. Seems fun, right? Those are "accidentals".
Answer: You are right, this piece is in the key of C major. There are countless songs written in C major, or any other major key that don't use any notes outside the major scale or any chords outside the 7 chords constructed from the major scale of the chosen key.
This piece is not one of them. The key of C major, or any other key is just that, a key. It is a tonal center that a piece of music revolves around. It is not a 7 note members only club. There are 12 notes available to us and any of the 5 notes that are not in the given key can be used at liberty by any composer for the desired effect they are trying to achieve in their compositions. There are no rules against composing with notes or chords outside a given key.
The reason the accidentals are there are because they were necessary for the composer to use to create the melodies and chords he came up with that you now hear when you listen to this theme. Those sounds could not exist without them. The accidentals create the interesting and intricate lines and chords you hear that are not available to us within a single major scale. Despite that that this piece does use the notes and chords of the key of C major a majority of the time.
Comment: @Usercomingsoon what do you mean by "formulate it"?
Comment: @Usercomingsoon If you want to play this piece you will probably have to practice it quite a bit depending on your skill level. If you are reading the music you don't have to memorize the chords, they are really only for reference. The only thing you hear is the two lines playing together.
Comment: @Usercomingsoon You can formulate a strategy for learning to play it. For example, learn one section and one hand at a time. Don't move on to the next section till you can play both hands together…or any other strategy that works for you.
Comment: There are some patterns of usage, that's the main focus of most of "Western Music Theory", but it's more similar to the "rules" of a spoken language, particularly as applied to poetry, than any engineering subject - the rules are more to help you figure out the right times to break them, and the "right" ways to interpret existing music, rather than being physical laws that all compositions must follow.
Comment: @Usercomingsoon Pretty much every phenomenon in music theory has a relationship to some kind of mathematical pattern, and those mathematical patterns are related to the physics of real-world oscillators. [The Wikipedia article _Otonality and Utonality_](https://en.wikipedia.org/wiki/Otonality_and_Utonality) might be a good starting point. But, as Tin Wizard observed, these things are the "grammar" of music: you still need to add the meaning, or you'll end up with some rather boring music that doesn't say anything. Music isn't _about_ instruments: it's about people.
Comment: @Usercomingsoon There is a great deal of logic behind the chord progressions. It's set in a ragtime style which has both classical and jazz elements to it. The first chord is a D9. The bass notes generally outline 1 and 5 of the chords. You'll find a lot of the weird notes are just suspensions: notes of the chord altered by a half-step that then resolve harmoniously into the chord. The more you listen and try to figure this stuff out, the more you'll notice the patterns. Keep practicing! This piece is pretty advanced! (There are some tricky 3:4 polyrhythms as well...)
Comment: Does that mean to play the Mario song I have to practice and memorize the chords rather than formulating it?
Comment: @phoog I am an engineer so I thought there might be some logic behind chord progressions. It seems like the musicians use hit and try to create music until something musical gets formed.
Answer: Adding to John Belzaguy's answer: The piece is pretty much straight C major, but it is using something we call chromatic approaches. This means that when playing a note you approach it from a half step below or above. This thus introduces harmony foreign notes that spice up the melody.
In the linked score you see this for example in measure 4 (B to A over Bb), measure 7,9,11 (G→F over F# and (D# before E), m8,12 (g# before a).
Then measure 13 uses some chords borrowed from c minor (Ab major, Bb major). Again, this just add a little bit of spice to the progression.
The part after that uses this contrast of this altered mediant Ab major and C major repeatedly.
Then again we get a section that is harmonically very clear C major, but again uses many chromatic approaches.
Answer: A particular key is defined far more by its 'home', 'place of rest', than which notes are included in the tune involved.
It's a partial truth that most melodies will mostly use diatonic (belonging to the key) notes, as they are all 'part of the specific set'.
However, what about when the composer wants to use one of the five other notes that 'don't belong'? What are they supposed to do then? There is a very, very small percentage of songs/pieces that don't have those chromatic notes in them, a lot of children's songs, for example. To keep them simple, but often on the edge of boring after a while!
So, including those extra notes is a necessity, but - if one in particular keeps on cropping up, and coincides with a different home place, then it's time to ask whether in fact the tune has been written in the correct key after all.

Sharps used in Super Mario theme


Add javascript to form by module

Question: i am writing a module in drupal 8. I wish to add a javascript when loading that form.
function ajax_example_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
 $form['#attached']['library'][] = array('ajax_example', 'ajax_example.sync_script');
}

and i have defined the following at the module library yml file
sync_script:
  version: 1.x
  js:
    js/test.js: {}

but seems the js is not embedded. And i have put the js in a folder called js at the root directory of the module.
What is the way for drupal 8 to embed a javascript at the form?
Answer: Update Feb. 2023: Hey friends! I am not writing Drupal modules for years. Please keep in mind I am not well up-to-date. This answer could not function as expected at this point in time, so edits are always welcome.

I don't know what you're aiming to build.
Because I think it's not good to use your JavaScript in hook_form_alter(), following that way, any form you try to see will invoke that JavaScript code.
However... Back to your issue. I think the hook_form_alter() function should be used in module_name.module file under the root of your module folder: modules/custom/module_name/module_name.module alongside the routing, info, etc.
After building the .module file, you need to build the module_name.libraries.yml file (if it's not already built), and there you write your routing requirements, for example:
<library_name>:
  version: 1.x
  js:
    js/<your_javascript_file>.js: {}
  dependencies:
    - core/jquery
    - core/drupalSettings

Back to the .module file, you implement the hook_form_alter, so your code goes like this:
/**
 * Implements hook_form_alter().
 */
function <module_name>_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
    $form['#attached']['library'][] = '<module_name>/<library_name>';
    $form['#attached']['drupalSettings']['<module_name>']['<library_name>']['variable'] = 'value';
}

To make sure, console.log your PHP variable in your_javascript_file to see it. For example:
(function($){
    console.log(drupalSettings.<module_name>.<library_name>.variable); // value
})(jQuery);

Hope this helps!
Comment: I think `route_name` would probably be better called `library_name`, since it has nothing to do with routing.
Comment: Please, use behaviors, see https://drupal.org/node/756722#behaviors
Comment: To alter only a single form, you can use implement `hook_form_FORM_ID_alter()`

Add javascript to form by module


drupal_add_js only once

Question: I have a field which is used in a nodeblock, and only appears on a few select pages. This however will vary throughout the lifetime of the website so I've been looking for a way to insert the Javascript this field requires only when it's present. Additionally, the field can appear more than once on any given page.
Looking at drupal_add_js, I can't see that it provides a way to only include JS once. Using this I was hoping that I could just call it in the field--field-name.tpl.php file for that field but as it occurs multiple times, the JS is included more than once.
I've looked at the module JS Injector, but that's just page specific and just calls the aforementioned function. This wouldn't help me because every time a new block is added somewhere, the list of pages including the JS would also need to be updated.
Is there a way I can include a JS file just once in Drupal?
Comment: Are you familiar with the php `static` keyword? If not put that in on google, it's ideal for solving your problem
Comment: A JS file once. @Clive I assume you mean something like `static $done; if(!$done) { drupal_add_js(); $done = true; }` ?
Comment: @Ben exactly :) although if you're adding files, Drupal will only add it once (the js array is keyed by the full path to the file to make absolutely sure of that)
Comment: Are you trying to add a JS file once or some inline JS once?
Answer: It depends what type of javascript you are adding.

drupal_add_js will only add files once, no matter how often it's called ;
Settings would overwrite previous values, so it's not a major problem ;
Inline code would indeed get added multiple times.

Given that your script needs including only once no matter how often the field is present, I assume you are not generating it's code on the fly - so putting it in a file would be an option.
Otherwise, as suggested by Clive, you could have your own wrapper around drupal_add_js that ensures a given inline js is only added once by using a static variable.
Comment: This is not strictly true. JavaScript settings don't overwrite previous values in Drupal 7 although it seems they now do in Drupal 8. https://www.drupal.org/node/1911578

drupal_add_js only once


How to create Liquibase changeset for below Postgres index creation statement - "using btree"

Question: create index if not exists department_index
   on department using btree (department_id);

How can I create a Liquibase changeset for the above Postgresql? I have to use "using btree" feature in order to attain the desired performance,
Comment: There is nothing special about this index. `btree` is the default if nothing else is specified. Just use ``
Answer: The default index is btree if you don't specify something else. You can use the createIndex entity to do this.
Reference:
Liquibase Documentation: createIndex


Perguntar se o usuário deseja mesmo sair da página?

Question: Tenho uma página onde ocorre uma task qualquer, só que é necessário finalizar ela, ou todo processo será perdido.
Gostaria de mostrar um alert, como o Facebook e outros sites mostram, perguntando ao usuário se deseja sair mesmo dessa página ou permanecer nela.
Comment: O facebook, como exemplo, apenas emite o alerta quando o usuário escreve algo nos comentários ou no status e não postou. Isso é para evitar  fechar a janela por acidente e perder o que escreveu. É sob esse contexto que vc quer ou é algo geral, independente da situação?
Comment: Exato, nesse contexto mesmo.
Comment: É possível fazer isso pro f5?
Answer: Use o window.onbeforeunload
Html:
<textarea id="comentario"></textarea>

Sair da página

Javascript:
window.onbeforeunload = confirmExit;
function confirmExit()
{
    if(document.getElementById("comentario").value != ""){
        return "Deseja realmente sair desta página?";
    }
}

Link do exemplo funcionando: JsFiddle
Comment: Você precisa só adaptar a sua lógica dentro da função, esse é apenas um exemplo, vou editar e adicionar um exemplo.
Comment: é a tal lógica que falta... do que jeito que postou é como uma resposta incompleta. Não está errado, mas está incompleto pois há um contexto específico.
Comment: Fiz a edição, apenas coloque o campo mudando o ID
Comment: +1, mas com este código ele dá o alert mesmo quando é clicado no botão pra enviar o formulário... acho que o ap vai ter o mesmo problema... tem como colocar esta exceção?
Comment: Obrigado amigo!
Comment: De nada, caso a resposta tenha ajudado no seu problema, selecione ela como resposta adequada para que outras pessoas possa encontrar caso tenha o mesmo problema
Answer: Sob o contexto de que o alerta deve ser exibido caso o usuário tenha um campo preenchido, segue abaixo um exemplo funcional com JQuery:

<input id="foo" type="text" value="" size="50" />

A pergunta não especifica o não uso de bibliotecas, por isso utilizei JQuery.
Answer: Coloque esse script na sua página e teste:


Paste from clipboard to excel file

Question: Is there a way to automate this process?
1- create new excel file
2- paste data which is ale+redy in clipboard to this new excel file with python
3- save this excel file
I have tried to find this function in openpyxl and xlsxwriter but there is only function for writing exact data to defined cells.
edit: the data in clipboard is the result from program run before this script
thanks
Comment: 1. writing to excel file using python: https://stackoverflow.com/questions/13437727/writing-to-an-excel-spreadsheet
2. pasting from clipboard using python: https://stackoverflow.com/questions/46817290/how-do-i-paste-the-copied-text-from-keyboard-in-python
Comment: can you give example of the data?
Comment: the data is combination of numerical data, empty spaces and letters. here is a link to google drive https://drive.google.com/open?id=184rE4FjB8JjHEMSs-E63tek-8GbuL8sb
Answer: This method gets data copied from the clipboard and converts it into a DataFrame. You can then select whichever column you want from the DataFrame and use the to_csv method to write to the Excel file.
<code>pandas.read_clipboard(sep='\\s+', **kwargs)
DataFrame.to_csv()
</code>
Check out the documentation :
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_clipboard.html
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
Comment: The OP wants probably a `XLSX` file, add `.to_excel(...` to your reference list.
Comment: thanks, but I still get the Traceback error Traceback (most recent call last):
  File "C:/Users/Luka/PycharmProjects/untitled2/novo.py", line 36, in
    pandas.read_clipboard(sep='\\s+', **kwargs)
TypeError: read_clipboard() argument after ** must be a mapping, not module

Paste from clipboard to excel file


Saving data for Windows Phone 8 app using vb

Question: Hi I am writing an app for windows phone 8 using vb and xaml. I have got all the fundimentals down but wish to store some data on the phone so that it is not lost when the application is reset. I have developed a number guessing game, I wish to store the users level and their coin balance on the phone then retrieve it when the application starts back up. I have found some references online how to do this in C# but nothing on vb. Could you please help?
Answer: If you only want to store a few values, you should make use of the IsolatedStorageSettings class. It will allow you to easily store key-value pairs in the Isolated Storage.
Sample VB.NET code taken from MSDN (link):
Imports System.IO.IsolatedStorage

Partial Public Class Page
    Inherits UserControl
    Private userSettings As IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings

    Public Sub New()
        InitializeComponent()
        ' Retrieve and set user name.
        Try
            Dim name As String = CType(userSettings("name"), String)
            tbGreeting.Text = "Hello, " & name
        Catch ex As System.Collections.Generic.KeyNotFoundException
            ' No preference is saved.
            tbGreeting.Text = "Hello, World"
        End Try
    End Sub

    Private Sub btnAddName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            userSettings.Add("name", tbName.Text)
            tbResults.Text = "Name saved. Refresh page to see changes."
        Catch ex As ArgumentException
            tbResults.Text = ex.Message
        End Try
    End Sub

    Private Sub btnChangeName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        userSettings("name") = tbName.Text
        tbResults.Text = "Name changed. Refresh page to see changes."
    End Sub

    Private Sub btnRemoveName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If userSettings.Remove("name") = True Then
            tbResults.Text = "Name removed. Refresh page to see changes."
        Else
            tbResults.Text = "Name could not be removed. Key does not exist."
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        userSettings.Clear()
        tbResults.Text = "Settings cleared. Refresh page to see changes."
    End Sub

End Class


Captar valor de input (Somente os campos numéricos) e colocar em Div

Question: Estou tentando criar uma função que pegue o valor de um campo input e coloque em div. Porém não estou conseguindo fazer funcionar.
Obs: Preciso pegar somente os valores numéricos.
Segue o meu código:

$( "#testar" ).click(function() {

    $('#txtDistancia').on('input', function () {
        var valor = $(this).val();
        $('#valorkm').html(valor);
    });
});

<input name="distancia" type="text" id="txtDistancia" value="7,5 km">

<input type="button" value="Testar" id="testar" class="btn btn-success" />

UPDATE: Eu gostaria que a pessoa testasse o código localhost, pois duas pessoas me ajudaram, eu vi o código funcionando aqui e também por meio do Fiddle, mas ao testar em localhost não funciona.
Comment: Aparece algum erro na consola do browser? esse javascript está definido no html depois do include do jquery certo?
Answer: Se você está testando pelo botão não precisa do on input. E para capturar somente os números pode usar:
match(/\d+/g)
Exemplo

$("#testar").click(function() {

  let valor = $('#txtDistancia').val().match(/\d+/g);
  $('#valorkm').html(valor);
});

<input name="distancia" type="text" id="txtDistancia" value="7,5 km">

<input type="button" value="Testar" id="testar" class="btn btn-success" />

Comment: @Wendell Na página que vc informou, a ordem das coisas estão erradas: a primeira coisa que vc deve carregar é a biblioteca jQuery, depois o HTML e por fim script. Vc alterou a ordem das coisas, por isso não funciona
Comment: eu tentei colocar sua solução no meu código, mas não funciona. [Veja aqui](http://galeria.net.br/teste/).
Comment: monte seu código num Fiddle @Wendell, pf
Comment: No Fiddle funciona, mas tanto localhost quanto no servidor não funciona. O que pode ser? [Veja o código no Fiddle aqui](https://jsfiddle.net/vs2b6mte/).
Answer: Tente assim:

$( "#testar" ).click(function() {
  var valor = $('#txtDistancia').val().replace(/[^0-9^,]/g, '');
  $('#valorkm').html(valor);
});

<input name="distancia" type="text" id="txtDistancia" value="7,5 km">

<input type="button" value="Testar" id="testar" class="btn btn-success" />

Comment: eu tentei colocar sua solução no meu código, mas não funcionou. [Veja aqui](http://galeria.net.br/teste2/)
Answer: Solução com javascript puro um regex para eliminar todos os caracteres nao-numeros

function Testar(){
  var distancia = document.getElementById('txtDistancia').value.replace(/[^0-9]/g,'');
  document.getElementById('valorkm').innerText=distancia;
};
<input name="distancia" type="text" id="txtDistancia" value="7,5 km">
<input type="button" value="Testar" id="testar" class="btn btn-success" onclick='Testar();' />

Caso deva retornar um numero com separador . (ponto)
Use o parseFloat para converter para Number, assim basta trocar a , por . para o parseFloat conseguir trabalhar com o numero

function Testar(){
  var distancia = document.getElementById('txtDistancia').value;
  var numero = parseFloat(distancia.replace(',', '.'));
  document.getElementById('valorkm').innerText=numero;
};
<input name="distancia" type="text" id="txtDistancia" value="7,5 km">
<input type="button" value="Testar" id="testar" class="btn btn-success" onclick='Testar();' />

O argumento de parseFloat deve ser uma string ou uma expressão de string. O resultado parseFloat é o número cuja representação decimal estava contida nessa string (ou o número encontrado no início da string que é o caso em questão quando trocamos a , por . ).


Subgroup with infinite index

Question: Let $G$ be group and $G$ has a subgroup $H$ with infinite index.
Is it true that the number of subgroups of $G$ containing $H$ is infinite?
Answer: No. I'm sure there are far simpler examples, but the first that comes to mind is a Tarski monster. This is an infinite group $G$ in which every nontrivial proper subgroup has order $p$, for some fixed prime $p$. Any such subgroup thus has infinite index, but is not contained in any subgroup except itself and $G$.
Comment: Tarski monster group is a very interesting.  Can you give me some reference about subgroup of this group?
Comment: What's wrong with the references cited in the Wikipedia article I linked?
Comment: You claimed that it is not contained in any subgroup except itself and $G$. I want to see more detail about this.
Comment: @Babgen: How many ways can one subgroup of order p contain another subgroup of order p?
Answer: I believe the following should provide a relatively simple example. Let $N$ be the additive group of the rationals, and $H$ the multiplicative group of the nonzero rationals. Let
\begin{equation}
  G = N \rtimes H
\end{equation}
be the semidirect product, in which $H$ acts on $N$ by multiplication.
Now $H$ is clearly a subgroup of infinite index in $G$. But $H$ is maximal in $G$, that is, there is no subgroup $L$ such that $H < L < G$. In fact, if $H < L$, then $L$ contains a nonzero $a \in N = \mathbf{Q}$, so if $b \in N = \mathbf{Q}$ is nonzero, then applying the element of $H$ that multiplies by $b a^{-1}$ we see that  $b = (b a^{-1}) \cdot a \in L$, so that $L$ contains $N$, and thus $L = G$.
Comment: What's wrong with the rather widespread $\,\Bbb Q\,,\,\Bbb Q^*\,$ ...?
Comment: Nothing @DonAntonio, but on the one hand I wanted to have an $H$ as in the original question, and then I wanted to make clear whether I was taking an element in one group or in the other.
Answer: In general, your result is false, but if $H$ is a normal subgroup, then it is true. Indeed, there is a bijection between the subgroups of $G$ containing $H$ and the subgroups of $G/H$; but $G/H$ has finitely many subgroups iff $G/H$ is finite iff $H$ is of finite index in $G$.
In particular, the result is true for abelian groups.
Comment: Let me ask, since I'm not the answerer here: why the downvotes?  Has the poster said something wrong, in which case a comment pointing out the mistake is called for?  Or is the observation correct but so irrelevant to the question that some feel a need to discourage this behavior?  Inquiring minds wonder...
Comment: I think some rather bitter serial downvoter is amongst us...Some people rush to downvote whenevever is, or seems to them to be, wrong.

Subgroup with infinite index


Is it better to compute Average Precision using the trapezoidal rule or the rectangle method?

Question: Background
Average precision is a popular and important performance metric widely used for, e.g., retrieval and detection tasks. It measures the area under the precision-recall curve, which plots the precision values for all possible detection thresholds against the respective recall values.
A typical precision-recall curve would look somewhat like this.

Confusion
Since there is only a limited number of thresholds to be evaluated on any given finite dataset, the precision-recall curve is a piece-wise linear function. Thus, I always thought the correct way of computing the area under that curve would be using the trapezoidal rule:
ap = sum( (recall[k+1] - recall[k]) * (precision[k+1] - precision[k]) / 2 )

Not only I thought so, apparently, since the official code for evaluating results on the Oxford Buildings dataset, which is a widely used benchmark in content-based image retrieval, computes AP in exactly this way, too.
However, the scikit-learn package for Python computes average precision differently in the function sklearn.metrics.average_precision_score, following the definition of AP from Wikipedia. They use the rectangle method:
ap = sum( (recall[k+1] - recall[k]) * precision[k+1] )

In the example given above, that would approximate the area under the precision-recall curve with the red function in the following figure:

The documentation of scikit-learn says the following about this:

This implementation is different from computing the area under the precision-recall curve with the trapezoidal rule, which uses linear interpolation and can be too optimistic.

One of the curators of the Oxford Buildings dataset, on the other hand, explained in a related question on StackOverflow that the rectangle method would be a "commonly used worse approximation".
Question
Bad enough that different benchmarks and different packages use different variants of Average Precision to compare methods, but now I wonder:

Which of the two versions is the "better" way of doing it? Trapezoidal rule or rectangle method?
What are the pros and cons for each?
What does the scikit-learn documentation mean with the claim the the trapezoidal rule is "too optimistic"?
Answer: For a piecewise linear function, using the trapezoidal rule with endpoints on each of the ends of the "pieces" will yield the exact area under the curve --- i.e., it is equivalent to integration under the curve.  This occurs when the trapezoids correspond exactly with the lines in the piecewise linear function.  (Of course, this does not hold if there are endpoints of the pieces in the function that are not endpoints of the trapezoids.)  Contrarily, the rectangular method will not give an exact area under the curve, though it should be close if you use a large number of rectangles.
As to which method is better, the exact method (trapezoidal) is better if it is computationally feasible.  I am not aware of any particular reason why it should not be computationally more expensive than the rectangular method, since the only difference is that it uses the average height of each endpoint instead of the maximum height.  If we partition the recall values using the endpoints $r_0 < r_1 < \cdots < r_n$ then we have:
$$\begin{align}
\text{Rectangular area}
&= \sum_{k=1}^n (r_k - r_{k-1}) \times \max (P(r_k), P(r_{k-1})), \\[10pt]
\text{Trapezoidal area}
&= \sum_{k=1}^n (r_k - r_{k-1}) \times \frac{P(r_k) + P(r_{k-1})}{2}. \\[6pt]
\end{align}$$
Assuming that these endpoints contain the endpoints of the piecewise linear function, it is simple to show that the trapezoidal area is the exact area under the curve.
Comment: Thank you for your answer! It was also always my opinion that the trapezoidal rule computes the exact area under the precision-recall curve. However, do you have any insight why a massively popular machine learning library such as scikit-learn would opt to use the right-point rectangular method instead then?
Answer: UPDATE
This journal article explains why linear interpolation is both "too optimist" and is also "incorrect," due to the non-linear properties of the Precision-Recall curve: https://www.biostat.wisc.edu/~page/rocpr.pdf

Due to the non-linear nature of the precision-response curve, linear interpolation results in erroneous overestimations.
With an averaging rule, missed changes in slope average out. With interpolation, they do not. Thus if points don't cover all the slope changes in the real curve, interpolation errors add up.  This is why interpolation is "too optimistic," and why the midpoint rule generally has half the error that the trapezoidal rule has.
https://math.libretexts.org/Courses/Mount_Royal_University/MATH_2200%3A_Calculus_for_Scientists_II/2%3A_Techniques_of_Integration/2.5%3A_Numerical_Integration_-_Midpoint%2C_Trapezoid%2C_Simpson%27s_rule
http://math.cmu.edu/~mittal/Recitation_notes.pdf
https://activecalculus.org/single/sec-5-6-num-int.html

This article is referenced as "[Davis2006]" in the scikit-learn documentation as the explanation as to why linear interpolation is inappropriate and "too optimistic" here.  See: https://scikit-learn.org/stable/modules/model_evaluation.html#precision-recall-f-measure-metrics
Also,
The function sklearn.metrics.average_precision_score does not use the rectangle rule, or any Riemann sum, right or otherwise.  It uses "average precision." The formulas are very different.

Note that f(x) is very, very different than Pi.
Due to the formulas for precision and recall, the Average Precision is actually computing an average, with discrete values between 0 and 1. Regarding Riemann, f(x) = y.  This gives you the height to multiply the delta with.  There is no averaging there.
Average precision is most analogous to the midpoint rule, as they are both doing averages.
Note that R uses the same formula for Average Precision: https://www.rdocumentation.org/packages/yardstick/versions/0.0.4/topics/average_precision
Comment: @Callidior - I made significant updates to my answer.  Average Precision is not a Reimann formula, but it is analogous to the midpoint rule.
Comment: Thanks for your answer! I understand the argumentation for the midpoint rule. scikit-learn, however,  uses the right Riemann sum instead of the midpoint rule. Why do you think this is not reflected in the figure? Please note that for computing AP, the intervals are not evenly spaced and there can are intervals with a delta of 0 (the vertical lines in the plot).
Comment: The link says AP is calculated using AP=sum(Rn-Rn-1)Pn.  I see other references that differentiate the rectangle rule from the midpoint rule, so I updated my answer.
Comment: For examples of what right Reimann sums look like, see Example 6 here https://www.math24.net/riemann-sums-definite-integral/ ; or Figure 4.2.6 here https://math.libretexts.org/Bookshelves/Calculus/Book%3A_Active_Calculus_(Boelkins_et_al)/4%3A_The_Definite_Integral/4.2%3A_Riemann_Sums
Comment: Thanks for the update and the extensive references. I understand from the provided literature, that linear interpolation is not a wise thing to do for precision-recall curves. It is worth noting that both papers referenced in the scikit-learn documentation propose other, more justified variants of AP, which are again different from the common implementation in scikit-learn and R.


How do I run Tukey test on 2 different types of categories R analysing questionnaire results

Question: I am trying to analyse the results from the Attrak Diff questionnaire, which is split into four 'quality' categories, and I was asking participants their opinions on three different feedback types. I got a significant p value from the one way repeated measures ANOVA so decided to run a Tukey test. The code I have for the Tukey test doesn't let me account for the second level of division though. So how can I run Tukey based on both quality and feedback, to be able to say stuff like 'participants considered the Spheres feedback to be more attractive than the Arrows feedback'? (Attractiveness is one of the categories of the Attrak Diff.) Thanks in advance!
library(lme4)
library(lmerTest)
library(multcomp)

attrakdiffdataframe <- read.csv("attrak diff.csv")

M <- lmer(value ~ quality+(1|feedback), data=attrakdiffdataframe)

M0 <- lmer(value ~ (1|feedback), data=attrakdiffdataframe)

anova(M0,M)

ANOVA <- anova(M0,M)
summary(ANOVA)

g <- glht(M,linfct=mcp('quality'='Tukey'))
summary(g)

Comment: Just a quick comment: you have "feedback" as a random effect grouping variable, not as a fixed predictor, so you haven't modeled the fixed effect of feedback or the interaction. That's why you can't get the post-hoc tests for feedback from the model you have specified. Try M<-lmer(value ~ quality*feedback + (1|participant), data=data) and then try to do the post-hoc comparisons (emmeans package may be easier to use for that than multcomp).
Comment: @Sointu that gives me this error:
Error in list2env(data) : first argument must be a named list
Does it mean I need to convert my values column to a list?
Comment: Did you put in the name of your own dataframe into the formula, or did you literally put in data=data? If so, putting in data=attrakdiffdataframe will probably fix this. If you already did that the problem is likely in your data structure, make sure it is in long format etc.
Comment: @Sointu I'm very embarrassed I missed that, thank you for the great spot! The sleep deprivation is starting to kick in :( I'm still not clear how to do the post-hoc comparison based on both quality and feedback. I'm happy to use whatever gets the job done package wise, but I haven't been able to find any clear examples for Tukey from emmeans.
Comment: That's an easy mistake to make. Do you want just the main effects of quality of feedback or their interaction too?
Answer: So, if you want the main effects of quality and feedback, and not the interaction, I suggest the following:
M<-lmer(value ~ quality + feedback + (1|participant), data=attrakdiffdataframe) #replace "participant" with the name of the variable that signifies your participant id
summary(M)

install.packages("emmeans")
library(emmeans)

emquality<-emmeans(M, pairwise ~ quality)
emfeedback<-emmeans(M, pairwise ~ feedback) #emmeans automatically adjusts for multiple comparisons using Tukey correction when used like this.

emquality
emfeedback

There's a separate issue that a multilevel model may be a bit of an "overkill" for your design, you could probably do well with a single-level regression with robust standard errors, or a generalized least squares regression, but this is to get you over the current issues :)


microblog module - problem with max characters

Question: On the left column on http://ruh.kz, you can the output of the Microblog module. This module uses the node title as a microblog entry; the maximum length of the title is by default 255 characters. In a microblog.module I've made the following changes:
<code>function microblog_form(&$node) {
  $type = node_get_types('type', $node);

  $form['title'] = array(
    '#type' => 'textarea',
    '#title' => check_plain($type->title_label),
    '#rows' => 4,
    '#cols' => 30,
    '#required' => TRUE,
    '#maxlength' => 500,
  );

  return $form;
}
</code>
Now, when I'm typing, it shows only 450 characters or less. Why it happens?
Answer: The title column of the {node} table is limited to 255 characters. Just because you change it in the UI doesn't mean that it can be saved like that in the database, unless you mess around with the database schema manually (and you should not do that).
I am not sure why that module is exposing itself as a node type, sounds rather strange to me. The only reason I can imagine is being able to rely on the node access system. But nodes are a rather heavy thing, there is a lot of overhead that seems unecessary to me for this use case.
Comment: u mean that module should not use node system? What's ur email, berdir. Want to discuss with you about your paid services :)
Comment: By the way, when you press "Сохранить" its "Save" on english → there's acting some kind of javascript - because it adds your microblog entry without reloading whole page.
Answer: Textareas do not support the #maxlength attribute see here, and also here.
As for why it's only showing 450 characters I'm not sure. Is there any JavaScript running from that module that restricts the amount of characters?
Comment: JS running from http://drupal.org/project/maxlength


Style a React component based on its ID

Question: So i've written a simple TODO with drag and drop and add, all the cards are generated using a function , my second column has an id to it and I now want any task added or dragged to it to have a css line through. How would I go about targeting it using the id

My code:
P.S Does not use create-react-app , just brought in through CDN and also uses react-dnd
let { DragDropContext, Draggable, Droppable } = window.ReactBeautifulDnd;

let lists = {};

let storeLists = () => {
  console.log("Writing Lists to Storage");
  console.log(lists);
  window.localStorage.setItem("listStore", JSON.stringify(lists));
  console.log("Set Correctly");
  lists = JSON.parse(localStorage.getItem("listStore"));
};

class Task extends React.Component {
  deleteTask = () => {
    let listId = this.props.listId;
    let index = lists[listId]["tasks"].findIndex((task) => {
      return task["taskId"] == this.props.inputTask["taskId"];
    });
    lists[this.props.listId]["tasks"].splice(index, 1);
    storeLists();
    this.props.refresh();
  };

  taskContentChanged = (event) => {
    let listId = this.props.listId;
    console.log(listId);
    console.log(lists[listId]);
    console.log(this.props.inputTask);
    let index = lists[listId]["tasks"].findIndex((task) => {
      return task["taskId"] == this.props.inputTask["taskId"];
    });
    console.log(index);
    lists[listId]["tasks"][index]["taskDescription"] = event.target.innerText;
  };

  keyPress = (event) => {
    if (event.key === "Enter" && event.target.className == "taskCardContent") {
      storeLists();
      event.target.blur();
      this.props.refresh();
    }
  };

  render = () => {
    return (
      <Draggable
        draggableId={this.props.inputTask["taskId"]}
        index={this.props.index}
      >
        {(provided) => {
          return (

                {" "}
                {this.props.inputTask["taskDescription"]}{" "}

          );
        }}
      </Draggable>
    );
  };
}

class List extends React.Component {
  addTask = () => {
    let listId = this.props.inputList['listId'];

    let newTaskInput = document.getElementById(listId + "Input");

    if (newTaskInput.value != "") {
      let newTask = {
        taskId: Math.random().toString(36).substring(2, 15),
        taskDescription: newTaskInput.value,
      };
      lists[listId]["tasks"].push(newTask);
      storeLists();
    }

    newTaskInput.value = "";
    this.props.refresh();
  };

  deleteList = () => {
    let listId = this.props.inputList["listId"];
    lists[listId] = undefined;
    storeLists();
    this.props.refresh();
  };

  listNameChanged = (event) => {
    let listId = this.props.inputList["listId"];
    lists[listId]["listName"] = event.target.innerText;
  };

  keyPress = (event) => {
    if (event.target.className == "newListItemInput" && event.key == "Enter") {
      this.addTask();
    }
    if (event.target.className == "listName" && event.key == "Enter") {
      event.preventDefault();
      event.stopPropagation();
      event.target.blur();
      storeLists();
      this.props.refresh();
    }
  };

  render = () => {
    let list = this.props.inputList;

    return (

          <input
            onKeyDown={this.keyPress}
            id={list["listId"] + "Input"}
            type="text"
            class="newListItemInput"
            placeholder="Add To List"
          />

{list["listName"]}

          <Droppable droppableId={list["listId"]}>
            {(provided) => {
              return (

                  {Object.values(list["tasks"]).map((task, index) => {
                    return (
                      <Task
                        index={index}
                        inputTask={task}
                        listId={list["listId"]}
                        refresh={this.props.refresh}
                      />
                    );
                  })}
                  {provided.placeholder}

              );
            }}
          </Droppable>

    );
  };
}

class App extends React.Component {
  state = {
    appLists: lists,
  };

  onDragEnd = (result) => {
    console.log(result);

    if (result.reason === "DROP" && result.destination) {
      let originListId = result.source.droppableId;
      let originIndex = result.source.index;
      let destinationListId = result.destination.droppableId;
      let destinationIndex = result.destination.index;

      let movedTask = lists[originListId]["tasks"][originIndex];
      lists[originListId]["tasks"].splice(originIndex, 1);
      lists[destinationListId]["tasks"].splice(destinationIndex, 0, movedTask);
    }
    storeLists();
    this.refreshApp();
  };

  refreshApp = () => {
    console.log("Refreshing...");
    this.setState({
      appLists: lists,
    });
  };

  render = () => {
    return (

        <DragDropContext onDragEnd={this.onDragEnd}>
          {Object.values(this.state.appLists).map((list) => {
            return (
              <List
                id={list["listId"]}
                inputList={list}
                refresh={this.refreshApp}
              />
            );
          })}
        </DragDropContext>

    );
  };
}

let startUp = () => {
  if (
    localStorage.getItem("listStore") &&
    !["undefined", "null"].includes(localStorage.getItem("listStore"))
  ) {
    lists = JSON.parse(localStorage.getItem("listStore"));
  } else {
    console.log("Creating List in Storage");
    lists = {
      f0ziqq7gfn: {
        listId: "f0ziqq7gfn",
        listName: "To Do",
        tasks: [
          { taskId: "fk8vs85v0wh", taskDescription: "Watch Regular Show" },
          { taskId: "kd3ve56f5fh", taskDescription: "" },
        ],
      },

      y0zitt47gfb: {
        listId: "y0zitt47gfb",
        listName: "Complete",
        tasks: [
          { taskId: "v0zigt47whc", taskDescription: "Create React Todo" },
          { taskId: "b0zi5t4rwhc", taskDescription: "Somehow get a PS5" },
        ],
      },
    };
    storeLists();
  }

  ReactDOM.render(<App />, document.body);
};

startUp();

Comment: Brainfart moment, just used that ID standardly , BURN OUT
Answer: Brainfart moment, just used that ID standardly , BURN OUT

Style a React component based on its ID


How to write in my page Exception Value with Django

Question: I would like to know how could a return to my page (and so write it) an Exception Value when it is raised.
For instance now I'm doing this:
try:
    user.save()
except:
    err_msg="An error verified during user adding process. Please repeate the procedure"
return render_to_response('staff.html', {'m':m, 'err_msg':err_msg})

I would like to be more clear to the user and told him what error is raised.
Answer: You should not show the actual error message to the user. That is bad in terms of security. What you have just done is fine. But you should figure out what was the reason for failure by printing actual error message for debugging purpose:
try:
    user.save()
except Exception, e:
    print e.message # print actual error message and figure out what went wrong
    err_msg="An error verified during user adding process. Please repeate the procedure"
return render_to_response('staff.html', {'m':m, 'err_msg':err_msg})

Answer: If you are asking, how to capture the text of the error message, just capture it and print it:
try:
   '2' + 2
except TypeError as e:
   print '{}'.format(e)

You can also print e.message, but its safer to try and print it directly.
If you don't know what exception is going to be raised, you can use a generic except clause, and then do this:
import sys

try:
   '2' + 2
except:
    print sys.exc_info()[1].message

However, a generic except clause is not good programming practice.  It is always good to catch exceptions explicitly.
Comment: In your example you have TypeError, in mine not, because I don't know what kind of error i could receive


Internal mail system

Question: How do I allow users send emails to the other users?
What module can I use to allow users to email each other?
Answer: I don't know about 'best' (although @Berdir might have something to say on the matter ;) ), but Privatemsg would probably be perfect for this:

Privatemsg allows your site's visitors to send private messages to each other.

It also provides e-mail notifications of new messages.
Comment: I don't know of a better alternative in Drupal, but I wouldn't say Privatemsg is the "best", that's quite a big word. There are many things that could be better :)
Comment: I used private messages on a dating site, works flawlessly, what other options exist?

Internal mail system


How to get NuGet packages to restore correctly for an existing project added from another solution?

Question: I have two Visual Studio 2019 solutions, A and B, with both containing several projects. Solution A includes one of the projects from solution B, ProjectB, which was added to A as an existing project. This project uses several NuGet dependencies. Obviously, NuGet packages are not under source control. When a user freshly checks out the solutions from source control, here's what happens:

If solution B is opened and built first, ProjectB's NuGet dependencies get restored as expected, and the project builds successfully.
If solution A is opened and built after 1., everything gets built successfully. However, if step 1 is omitted, ProjectB fails to build within the scope of solution A because its referenced dependencies don't get recreated in the original path, despite NuGet having installed all the dependencies in A's packages folder. This results in plenty of "The referenced component could not be found" error messages, and a situation equivalent to this one.

Simply cleaning the NuGet cache doesn't help. The only thing that seems to resolve the issue is to call
Update-Package -reinstall -ProjectName ProjectB

from the solution A's Package Manager Console prior to triggering a (re)build.
I wanted to automate this by including the Update-Package command in a pre-build event, but apparently Package Manager Console is not accessible there, and I'd like to avoid installing any other software.
Is there a way to correctly get all project dependencies restored in such case, so that the entire solution A would build from scratch automatically?
Answer: Sounds like you need to add a Nuget.Config file as a Solution Item to each solution and add the "respositoryPath" config key to the file so the two solutions are resolving Nuget package references to the same folder:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
     <add key="repositoryPath" value="../../Nuget/packages" />
  <config>
</configuration>

Obviously, you'll want to adjust the repositoryPath value for each Nuget.Config file so the same path is used for both solution (if Solution A is one folder lower in the file structure than Solution B, you'll need to add another ../ to the path).
After you add the Nuget.Config file to each solution, you need to close/re-open each solution and then run Update-Package -reinstall to ensure the reference paths are updated in each project file.
Answer: It turns out that you can use PackageReference format inside a project instead of packages folder, and doing so resolves the original issue directly without any extra action needed. So the most elegant solution is to migrate from packages.config to PackageReference: right-click on the problematic project's References in solution explorer, then click Migrate packages.config to PackageReference in the context menu. From now on, NuGet dependencies will resolve correctly when a project is included in another solution.


Same authentication in 3 different web applications, that have their own database

Question: I have three REST web applications (Java) with their own database (MongoDB).
All the applications require authentication, so I created a collection called User in each database.
Now I want the same user to be able to log in each application.
Shall I have a fourth shared database only for user?
Shall I have a sort of synchronization process of the collection User among the three databases?
What's the solution?
Answer: I think the solution is to develop some microservice (OAuth2 for example) to do the authentication and implement all login and permission check routines by its means.
If that's an overkill for your task, it is better to make a separate DB for user accounts and synchronize application databases against it.


laravel4 Response::json(array()); not work in hosting

Question: HOSTING
I tried to response json in hosting with follow the example http://laravel.com/docs/responses#special-responses
return Response::json(array('name' => 'Steve', 'state' => 'CA'));

but response headers is

and response: Reload the page to get source for: httx://xxxx.com
So, I'm try echo Response::json(array(...)); result is

I must to use it work:
header('Content-Type: application/json');
echo json_encode($data);

MY LOCAL
and my localhost Response::json(array()); is work! version of the same
What should I do?
help me please.
Comment: Can you see what is installed on your hosting vs local using  'application/json'));
Comment: hosting is use PHP Version 5.4.15 same me in local. I think it work for laravel
Comment: I had tried it. But was ineffective.
Comment: It *may* be related to Varnish, but I can't say what to do to determine if it is and how to solve the issue.
Comment: With the few information you've provided, I can say somewhere before `return Response::json ...` your PHP script is sendign a header to the browser.
Comment: can you post the controller function? Also the JS function please. I had a similar problem with a cheap hosting: changing hosting provider fixed the problem
Answer: When you use Response::json($yourarray); you are going to be sending a header with the json data.
If you are not using jquery or javascript to accept the response, and are instead trying to pass data to another method, try:
return json_encode($yourarray)
Answer: This may solve the problem:
return Response::json(array('name' => 'Steve', 'state' => 'CA'))
               ->header('Content-Type', 'application/json');


How many different ways are there to place a white and a black rook on a chessboard?

Question: How many different ways are there to place a white and a black tower on a chessboard so that they
a) attack each other?
b) do not attack each other?
c) What would be the solution of a) and b) if the two rooks were black?
Comment: By "tower" you mean [rook](https://en.wikipedia.org/wiki/Rook_(chess)) (= Tauer in German), right?
Comment: it depends on what's considered a different way. Is a board that is equivalent to another board rotated distinct? what if it's reflected? etc.
Answer: a) There are 28 edge locations. For each edge location there are $7 \times 7$ ways to place the opposing rook such that it does not attack the other.
There are 36 non-edge locations. If a rook is placed on the square $(a,c)$ (I'm not using the proper chess notation of $F8$ or $G4$) for $a, c \in \{2, 3, ... , 6, 7\}$ think about how many ways there are to place the other rook:
Four rectangles of size $(a-1) \times (b-1), (a-1) \times (8-b),  (8-a) \times (b-1), (8-a) \times (8-b).$
This is
$$(ab  -a -b +1) + (8a -ab -8 + b) + (8b - 8 - ab + a) + (64 - 8b - 8a + ab)$$
Cancel the $ab$ terms
$$ =(-a -b +1) + (8a -8 + b) + (8b - 8  + a) + (64 - 8b - 8a )$$
Cancel the $a$ and $b$ terms
$$ =1 + -8 - 8  + 64 = 49 = 7 \times 7.$$
Evidently the number of available spaces doesn't change based on whether or not you are on the edge (this is easily seen by just visualizing a rook on the board).
So there are $64 \times 49$ ways.
b) Just take the total number of combinations: $64 \times 63 - 64 \times 49 = 64 \times 14$ (again as expected).
c) How would this change the problem?
Comment: You have parts a and b backwards.
Comment: All that algebra to prove that a rook on any square attacks 14 squares, 7 on its rank and 7 on its file?


Simplifying with boolean algebra.

Question: I have two parts to my question, the first being how do I use Boolean algebra to reduce A'C'+ABC+AC' to have only 3 literals. I sort of understood the concept where you can factor things out but for some reason it won't come to mind with this one. My next question is how do I use DeMorgan's law with (a+c)(a+b')(a'+b+c').
I don't necessarily need the answers, I need to know the process to get the solution.
Edit:
For DeMorgan's law, I'm not really sure if this is right but I multiplied out all 3 and got aa'+aa'b'+aa'c+a'b'c+ab+abb"+abc+bb'c+ac'+ab'c'+acc'+b'cc'.
If that is right then the next step would be to simplify correct?
Comment: Although it is reasonable to use "multipication" to mean logical *and* with addition to mean logical *or*, one should state that this is the intended interpretation.  Less ambiguous would be to use actual mathematical notation $\land$ for logical *and* (resp. $\lor$ for logical *or*).  See this introduction to [using mathematical expressions](https://math.stackexchange.com/help/notation).
Answer: HINT
For the first problem focus on the terms $A'C'$ and $AC'$
You can do:   $A'C' + AC' = (A' + A)C' = (1)C' = C'$
So as the first few steps you get:
$A'C' + ABC + AC' = $
$A'C' + AC' + ABC = $
$(A' + A)C' + ABC = $
$(1)C' + ABC = $
$C' + ABC$
Now, do you see how you can simplify this just a little bit more?
Comment: Is it right if I use the complement for A' and A and for C' and C from ABC so I end up with (1)(1) + AB + (1)C' and ends up with AB + C'
Comment: @15quince15 That actually ends up being correct ... but I am unsure if you are really thinking about that the right way ... In fact, you're using a pretty advanced version of a Reduction rule there which goes a good bit beyond the 'standard fare' of Boolean Algebra.  I'll add a little bit to my post to help you think about this with more elementary principles.
Comment: thank you very much for your help, I understood what you are doing by arranging it and separating it then using the complement you are able to get to that point. So to continue from here you would do (C' + AB)(C'+C) then (C'+AB)(1).
Comment: @15quince15 Exactly! Step by simple step typically does the job :)

Simplifying with boolean algebra.


If I have a 16.67% fail rate (N=24) & I do another 24 tests, what is the likelihood that I get 0 fails by chance?

Question: Doing some code testing and I have a pre-fix environment where I ran 24 test scenarios and 20 out of 24 worked as expected—only 4 (16.67%) failed.
In the code where the fix exists, if I do another 24 tests, what are the chances that I get a 100% pass rate even if the code didn't fix the problem?
I feel like I probably need to do more tests but I can't remember my stats course and was hoping someone here could help
Comment: No, actual work work—I'm doing QA testing for code & trying to figure out just how hard I have to test this to be confident the code fixes the problem
Comment: Is it a homework?
Comment: If these are 24 _different_ scenarios, then you don't have _nearly_ enough data to even start an analysis. If they're the same, we can sort of guess the values. Can you clarify?
Comment: "What are the chances that I get a 100% pass rate even if the code didn't fix the problem?" Unless your tests are not deterministic, there's a 0% chance of this happening. I don't think using probability to approach this is the right idea; you should formalize the bug you're trying to fix, and create tests directly relating to it. You could write 100 more tests that will pass post-fix, but without analysing the tests themselves, that result is meaningless, as they might be testing the wrong thing.
Comment: To expand on what some other comments indicated: Statistics, fundamentally, argues about probabilities surrounding random events. Why do you believe that any of these events are random?
Comment: Because my devs have not given any root cause/valid repro steps to always cause a fail on purpose, I'm stuck doing random tests in an environment that doesn't have their fix & hoping for failures

Given that I saw only 4 failures out of 24 (and later 32) tests pre-fix, I knew I would have to do a LOT of post-fix tests to say with any confidence the fix worked.
Comment: However, devs realized that there was a different part of code that wasn't up to date in my pre-fix environment. They refreshed that & I did my same 32 tests this morning and got 17 failures.

Now, assuming this is a hypergeometric distribution, I can do just 16 post-fix tests and have a 99.99% confidence the fix worked (if I don't get any errors in the 16 post-fix tests)
Answer: One straightforward way to analyze this situation is to assume for testing purposes that the fix made no difference.  Under this assumption, you may view the assignment of the (potential) 48 observations into pre-fix and post-fix groups as being random.  If (hypothetically) all the post-fix outcomes work as expected, it means you have observed 44 expected outcomes and 4 "failures" out of 48 (presumably) independent observations.
This permutation test frames your question as "what is the chance that a random partition of 48 test scenarios, of which four are failures, places all four failures in the first group?"  Because all such random partitions are equally probable, this reduces the answer to a simple counting calculation:

There are $24\cdot 23 \cdot 22 \cdot 21$ equally-likely ways to place the four failures within the sequence of $24$ pre-fix scenarios.
There are $48\cdot 47\cdot 46 \cdot 45$ equally-likely ways to place the four failures within the sequence of all $48$ scenarios.

Ergo, under these hypotheses the chances that all four failures occurred before the fix are
$$\frac{24\cdot 23 \cdot 22 \cdot 21} {48\cdot 47\cdot 46 \cdot 45} = \frac{77}{1410} \approx 5.5\%.$$
The interpretation could be written in the following way:

Suppose four of 24 initial scenarios resulted in failure and none of 24 post-fix scenarios resulted in failure.  This had a $5.5\%$ chance of occurring when the fix changed nothing. Because $5.5\%$ is small, it provides some evidence that the fix worked; but because it's not really that small--events with those chances happen all the time--it's probably not enough evidence to convince anyone who is sceptical that the fix worked.  Unless collecting more data is very expensive, it might not be enough evidence to support consequential decisions related to the fix.

Even if the fix has already occurred, you have the flexibility to collect more post-fix data.  You can experiment with analogous computations to see how many more post-fix scenarios you need to run (all without failure) in order to establish a convincing level of evidence that something changed for the better after the fix.  For instance, if you need this chance to be less than $1\%,$ you would want to run $49$ post-fix scenarios rather than $24.$
Of course, as soon as one post-fix scenario produces a failure you would likely stop your testing and try to improve the fix.  Sophisticated versions of this approach (where you stop once you have enough information, without trying to guess how much you need beforehand) are called sequential testing procedures.
Comment: @Ponch While I think whuber's answer to the posted question is pitched perfectly, your extended questions in followup suggest that it may be worth noting that the relevant distribution here is called the [hypergeometric distribution](https://en.wikipedia.org/wiki/Hypergeometric_distribution), and with which many programs can do the calculations for you, perhaps making it a little easier to figure out whatever you're trying to work out. It may save you some effort. e.g. Excel has a function for it.
Comment: It takes a little effort with each new program too, because they all seem to implement the function in slightly different ways (e.g. R's equivalent function, `dhyper`, works differently (but both give the same answers when used correctly). I am glad you managed to sort it out.
Comment: I like this... And to make sure I understand, running 36 post-fix scenarios I'd have a ~97.8% confidence all four failures weren't just in the pre-fix scenarios by chance!
Comment: Yes, that's the number I get.
Comment: I might have to do more pre-fix tests, so let me see if I TRULY understand the math...

Let's say I do another 8 pre-fix tests and see 3 new errors. Now with N = 32 I have 7 errors or a 21.9% fail rate

If I then do 24 additional post-fix tests and see 0 errors in those 24, I'd have ~98.5% confidence the fix worked because there would only be a 1.45% chance the 7 errors were localized within the first 32 by chance:

(32⋅31⋅30⋅29⋅28⋅27⋅26) / (56⋅55⋅54⋅53⋅52⋅51⋅50) ≈ 1.45%

(It's sad I was a math major in college, but I always had trouble with stats!)
Comment: That's right, you got the pattern.
Comment: @Glen_b I love learning new Excel tasks! It took some work but I think I figured it out.

HYPGEOM.DIST(4,4,24,48,FALSE) would be the original ask - what is the likelihood of there being 4 fails and finding all 4 fails in the first 24 out of 48 attempts (Excel spits out 0.054609929 or ≈ 5.5%)

HYPGEOM.DIST(7,7,32,56,FALSE) is the new hypothetical ask of finding all 7 fails in the first 32 of 56 tests (Excel spits out 0.014513167 or ≈ 1.45%)!

This is great. Thank you both so much!!


Authentication to Azure Storage Account and Azure Service Bus using managed identity takes longer than using connection string

Question: I have a simple Azure function that got triggered on Service Bus Queue, downloads data from Storage Account, performs some simple logic and sends message to another Service Bus Queue.
Function was using connection strings (Shared Access Key) to authenticate.
To follow best practices and recommendations I changed the authentication method to be managed identity.
After this change, I noticed a significant decrease in performance = > download data from storage account and send message to queue takes 2.5 times longer.
As an example I created two simple functions. Both got triggered from service bus queue and download content from blob, the only difference is authentication method. Runtime of the first one is significantly less that second one.
 [FunctionName("TestFuncWithConnString")]
    public async Task Run(
        [ServiceBusTrigger("testconstring", Connection = "ServiceBusConnectionString")] ServiceBusReceivedMessage message,
        ServiceBusMessageActions messageActions)
    {

        var storageConnectionString = _configuration.GetConnectionString("StorageConnectionString");
        var blobContainerClient = new BlobContainerClient(storageConnectionString, "containerName");
        var blobClient = blobContainerClient.GetBlobClient("XXX");
        var blobMessage = await blobClient.DownloadAsync();

        await messageActions.CompleteMessageAsync(message);
    }

    [FunctionName("TestFuncWithManagedIdentity")]
    public async Task Run(
        [ServiceBusTrigger("testmanagedidentity ", Connection = "SBNamespaceFQName")] ServiceBusReceivedMessage message,
        ServiceBusMessageActions messageActions)
    {

        var storageConnectionString = new Uri($"https://{StorageAccount}.blob.core.windows.net/");

        var blobServiceClient = new BlobServiceClient(storageConnectionString, new DefaultAzureCredential());
        var blobContainerClient = blobServiceClient.GetBlobContainerClient("containerName");
        var blobClient = blobContainerClient.GetBlobClient("XXX");
        var blobMessage = await blobClient.DownloadAsync();

        await messageActions.CompleteMessageAsync(message);
    }

Does someone have any idea what might be wrong here?
Comment: I agree with Guarav that we'll need to see your code to help further.  How you're creating and managing the clients will be a large part of your performance characteristics.  Generally speaking, it is expected that managed identity authorization takes longer than a connection string, as both the client and service have to coordinate with AAD, where a connection string is validated entirely by Service Bus itself.
Comment: Are you noticing the slowness for every request or just the first request? Please edit your question and include the code as well.
Comment: It happens not only for first request.
Comment: Please share your Function's code.
Comment: We run some perf tests and got the avg_duration different for the same function with only difference in authentication  - managed Identity vs connection string authentication. Added to my question snapshot. To share function Code is not straight forward and requires some refactor. If there is no other way to figure it out will try to do it
Comment: I created as example two functions, you can se details in the question. I agree that it might take longer because of AAD coordination, but 2.5-3 times longer is expected?
Answer: Generally speaking, it is expected that managed identity authorization takes longer than a connection string, as both the client and service have to coordinate with AAD, where a connection string is validated entirely by Storage itself.
Because you are creating your BlobServiceClient and DefaultAzureCredential on each invocation of the function, you're paying the cost to acquire the token and authorize on each call.  Azure SDK guidance encourages treating clients as singletons.  (see: Lifetime management for Azure SDK .NET clients)  Doing so would reduce the number of times that you need to authenticate and authorize, avoiding the need to pay that cost so frequently.
The recommended approach would be to use Dependency injection for Azure Functions with the AddBlobServiceClient extension defined by the Azure Blob Storage package.  Alternately, you could create a static instance of the BlobServiceClient as part of your function class - though you are likely to see that be less efficient over time.
Comment: Example above is just for demo. I agree that connection should be reused. I just wanted to understand if it is acceptable that authentication via managed identity takes 3 times longer. Thank you for confirming that this delay is expected.


Computing consecutive $p$ Bell numbers modulo $p$ (a prime)

Question: The origin of this question is the conjecture on the period $T_p$ of $n\mapsto B_n\bmod p$ for a prime $p$. [As of now, the question stands alone - the connection with the conjecture has lost its meaning; see the linked question.]
Touchard's congruence $B_{n+p}\equiv B_n+B_{n+1}\pmod{p}$ implies that $T_p$ (exists and) is a divisor of $$N_p=(p^p-1)/(p-1)$$ (the conjecture says $T_p=N_p$ for all $p$), and that, to test whether $m$ is a period of $n\mapsto B_n\bmod p$, it suffices to check $B_{m+n}\equiv B_n\pmod{p}$ for $0\leqslant n<p$. Which brings the following.

Given a prime $p$, how quickly (in terms of $p$) can one compute $$(B_0,B_1,\ldots,B_{p-1})\bmod p\tag{1}\label{initial}$$ and, more generally, $$(B_n,B_{n+1},\ldots,B_{n+p-1})\bmod p\tag{2}\label{shifted}$$ for an integer $n<N_p$ (or at least for a divisor of $N_p$)?

Of course, the verification of $T_p=N_p$ this way requires complete factorization of $N_p$, which is out of this question. (Still, in hope of a counterexample, one can try partial factorizations.)
The article referenced within gives simple algorithms. For $\eqref{initial}$, it is basically the triangular scheme requiring $\mathcal{O}(p^2)$ additions in $\mathbb{Z}/p\mathbb{Z}$, and for $\eqref{shifted}$, it is based on $B_{n+p^m}\equiv mB_n+B_{n+1}\pmod{p}$, a consequence of Touchard's, giving an $\mathcal{O}(p^2\log n)$ algorithm (again, counting operations in $\mathbb{Z}/p\mathbb{Z}$).
But we can do better. My own attempts are in an answer below.
Answer: For $(1)$, we have the following identity in $\mathbb{F}_p[x]$, where $\mathbb{F}_p=\mathbb{Z}/p\mathbb{Z}$: $$\sum_{n=0}^{p-1}B_n x^n=x^{p-1}+\sum_{n=0}^{p-1}x^n\prod_{k=n+1}^{p-1}(1-kx).$$
It is obtained from the formal power series $\sum_{n=0}^{\infty}B_n x^n$${}=\sum_{n=0}^{\infty}\prod_{k=1}^{n}\frac{x}{1-kx}$ using the identity $\prod_{k=1}^{p-1}(1-kx)\equiv 1-x^{p-1}$. (My initial idea was to use $\sum_{n=0}^{\infty}B_n x^n/n!=\exp(e^x-1)$ with fast composition; the above is a better one.) Rewriting, $\sum_{n=0}^{p-1}B_n x^n=x^{p-1}+Q_{0,p}(x)$, where $$P_{u,v}(x)=\prod_{k=u}^{v-1}(1-kx),\qquad Q_{u,v}(x)=\sum_{n=u}^{v-1}x^n P_{n+1,v}(x),$$ and, for $u\leqslant v\leqslant w$, we have $$P_{u,w}(x)=P_{u,v}(x)P_{v,w}(x),\qquad Q_{u,w}(x)=Q_{u,v}(x)P_{v,w}(x)+Q_{v,w}(x),$$ which gives a divide-and-conquer approach. Assuming multiplication of degree-$d$ polynomials done in $\mathcal{O}(d\log d)$ operations, this results in an $\mathcal{O}\big(p(\log p)^2\big)$ algorithm.

For $(2)$, let's reformulate Touchard's congruence in operator terms. Consider the vector space (over $\mathbb{F}_p$) of all sequences in $\mathbb{F}_p$, its subspace $\mathscr{B}_p$ generated by $e_k : n\mapsto B_{n+k}\bmod p$, and the "step operator" $S$ on $\mathscr{B}_p$ that sends $e_k$ to $e_{k+1}$ for each $k$. Then the congruence says $S^p=S+I$, where $I$ is the identity operator. So, the arithmetics of polynomials in $S$ is the one of $\mathbb{F}_p[x]$ modulo $x^p-x-1$; in particular, $S^{p^m}=S+mI$ (already stated in the OP) and, more generally, $$n=\sum_{k=0}^{d}n_k p^k\implies S^n=\prod_{k=0}^{d}(S+kI)^{n_k}.$$ Again, this can be computed by divide-and-conquer, this time in $\mathcal{O}\big(p(\log p)^3\big)$ operations.


Low rank approximation of binary valued matrix

Question: How does one get the low rank approximation of binary matrix? Is the low rank approximation also a binary matrix?
Note - Here binary matrix just means that any entry of the matrix can either be 0 or 1.
Answer: A naive approach might be using a plain ol' SVD with thresholds. Choose the thresholds that maximize classification accuracy, or some other sensitivity/specificity tradeoff.
Thresholding might up the rank, but it begs the question of why one would need to a low rank approximation of a binary matrix. Usually, low rank approximations are convenient for scale development and identification of subscales which is strongly based on assumptions about additive variance (this holds for normally distributed random variables but not Bernoulli random variables).
To me, I would consider creating separate models for the marginal cell counts as well as the odds ratios in a log linear model describing the assocations. That would create a large number of parameters, obviously. If I wanted something that were computationally more efficient and "easier to digest" I might consider using sparse estimation with a LASSO or other tool.
Comment: And what about this part of the OP: "Is the low rank approximation also a binary matrix?"
Comment: @amoeba, no, it would not be binary. They apply thresholds at the end
Comment: "Thresholding might up the rank, but it begs the question of why one would need to a low rank approximation of a binary matrix." The classic example of SVD approximations to binary matrices is the Netflix Prize data: an enormous matrix of users and movies can be decomposed into rich feature vectors describing genres of movies and the preferences of users using SVD.
Answer: One idea is to compute the Hamming distance between data vectors and then use this as a pairwise distance metric. If the number of dimensions is much larger than the number of data points, this may make sense. I am not sure if this reduces rank for certain, but it's a more compact representation of the structure.