How to assign an Integer to a item in list – Python card game

I am new to python and I have been having some trouble making this card game In between also known as Acey Duecy.
I have made my deck of cards using a list with the code :

cardname = ("ACE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN","JACK","QUEEN","KING")
suit = ( "OF CLUBS","OF DIAMONDS","OF HEARTS","OF SPADES")
deck = ()
for n in range(4):
    for i in range(13):
        all_card = (cardname(i) + " " + suit(n))
        deck.append(all_card)

After assigning a integer to the item in the list, I would like to remove the 2 cards if they are the same, but since I havent assigned them numbers and they are just strings right now the code does not work. I would also not want to have to assign each 52 cards the value manual since in my game the suits don’t matter thus its just the Ace, two, three etc.

first_card = deck(0)
second_card = deck(1)

if first_card == second_card :
    print(".pop = False")
    deck.pop(0)
    deck.pop(0)
    print(deck)
else :
    print("They are not the same card")
```

python – Integer division when implementing Karatsuba’s algorithm

So im currently implementing Karatsuba’s algorithm in python and I cam across a very odd issue. I will provide two sets of code and I believe that they both theoretically do the same thing, but provide different answers (one right and one wrong).
The first set of code:

def decompose(x, n):
    a = int(((x)//10**(n)))
    b = int(x % (10**(n)))
    #print("a", a, "t", "b", b)
    return a, b


def digits(x):
    return len(str(x))

# both x and y are n digit numbers


def multiply(x, y):
    n2 = min(digits(x), digits(y))//2
    if x < 10 or y < 10:
        return x*y
    a, b = decompose(x, n2)
    # print("a", a, "t", "b", b)
    c, d = decompose(y, n2)
    # print("c", c, "t", "d", d)

    if min(digits(a), digits(b), digits(c), digits(d)) == 1:
        ac = int(a*c)
        bd = int(b*d)
        adbc = int((a+b)*(c+d) - a*c - b*d)
    else:
        ac = multiply(a, c)
        bd = multiply(b, d)
        adbc = multiply(a+b, c+d) - ac - bd

    return (10**(2*n2))*ac + (10**(n2))*(adbc) + bd


x, y = 3141592653589793238462643383279502884197169399375105820974944592, 2718281828459045235360287471352662497757247093699959574966967627
print(multiply(x, y))
print(x*y)

Second set of code:

def decompose(x):
    n = digits(x)
    a = int(((x)//10**(n//2)))
    b = int(x % (10**(n//2)))
    #print("a", a, "t", "b", b)
    return a, b


def digits(x):
    return int(len(str(x)))

# both x and y are n digit numbers


def multiply(x, y):
    n2 = min(digits(x), digits(y))//2
    if x < 10 or y < 10:
        return x*y
    a, b = decompose(x)
    # print("a", a, "t", "b", b)
    c, d = decompose(y)
    # print("c", c, "t", "d", d)

    if min(digits(a), digits(b), digits(c), digits(d)) == 1:
        ac = int(a*c)
        bd = int(b*d)
        adbc = int((a+b)*(c+d) - a*c - b*d)
    else:
        ac = multiply(a, c)
        bd = multiply(b, d)
        adbc = multiply(a+b, c+d) - ac - bd

    return (10**(2*n2))*ac + (10**(n2))*(adbc) + bd


x, y = 3141592653589793238462643383279502884197169399375105820974944592, 2718281828459045235360287471352662497757247093699959574966967627
print(multiply(x, y))
print(x*y)

In the first set, I pass n2, where n2 = 32 because x and y have 64 digits. In this case I get the correct answer for the product of x and y. However, in the second set of code, I manually find the number of digits and then divide it by 2 using integer division, which should also give 32 but gives me the wrong answer. Any help would be appreciated.

9 – Set a default integer value with #ajax, according to a form state value in the same form

JavaScript API / AJAX Forms is quite complete and good. Plus, there are many examples around.

Anyway, tthere are various errors.

  1. The ajax callback should be attached with code like $form('your_field')('widget')('#ajax')= (...);

  2. You should set the wrapper using an ID that’s kept the same even after AJAX calls. A common solution can be to set up it using #prefix and #suffix

    $form("field_second_field")("#prefix") = '<div id="my-id">';
    $form("field_second_field")("#suffix") = '</div>';
    
  3. The AJAX callback should return a renderable array or an AJAX command; in this case, return $form('field_second_field')

Written as the following, it should work.

function my_module_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  $form('your_field')('widget')('#ajax') = (
    'callback' => 'custom_callback',
    'event' => 'change',
    'wrapper' => 'custom-id',
  );

  $form("second_field")("#prefix") = "<div id='custom-id'>";
  $form("second_field")("#suffix") = "</div";
}

function custom_callback(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  //your logic
  return $form("second_field");
}

Please note, I never changed the #default_value inside the AJAX callback, but I see no reason why it should not work.

hooks – Drupal 9 Set a default integer value with #ajax, according to a form state value in the same form

There is an integer field: “Number of pitches”
Another integer field: “Maximum people allowed”.
These will usually be the same values.

I am trying to set the value of the field specified in the pitches, as the default value for the maximum number of people.Whatever combination I try, no results. In a hook_form_alter I try to realize this with Ajax.

function zaal_condities_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
if($form_id == 'node_bedrijf_form' || $form_id == 'node_bedrijf_edit_form'){
   $form('field_staanplaatsen')('widget')(0)('value')('#ajax') = (
      'callback' => 'zaal_condities_AjaxCallback',
      'event' => 'change',
      'wrapper' => 'edit-field-staanplaatsen-0-value',
    );
  }
}

function zaal_condities_AjaxCallback(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  if($form_state->getValue('field_staanplaatsen') != false){
    $inputValue = $form_state->getValue('field_staanplaatsen');
    return $form('field_bezetting_tot')('widget')(0)('value')('#default_value') = $inputValue;
  }
}

database design – better than ongoing integer and uuid as primary key

I agree entirely with your approach.

Internally within the database, auto-incrementing integers work just fine.

But …

Externally-visible tokens should be UUIDs, or something similar. (Your random-string is fine.) It’s a so-called “moniker,” or “handle.” These values should be stored in a separate field with a UNIQUE index. In true “third normal form” parlance, they should contain no embedded information, and they should be “un-guessable.” A purely-arbitrary, intrinsically meaningless string.

External (URL…) references should always reference this field. You should never publish “actual database primary keys” to the outside world.

Writing char buffer in integer variables using c++. How to avoid writing the same piece of code for every variable?

I am trying to convert a char buffer into multiple int variables. This is what i got so far after a day of trying.

ZeroMemory(buf, 4096);
            int bytesReceived = recv(sock, buf, 4096, 0);
            if (bytesReceived > 0)
            {
                /*for (int i = 0; i < bytesReceived; i++)
                {
                    cout << hex << (int)buf(i);
                }*/
                std::stringstream ss;
                std::stringstream aa;
                for (int i = 0; i < 2; i++)
                    ss << std::hex << (int)buf(i);

                unsigned result;
                ss >> result;
                int Number1 = result;
                cout << Number1 << "n";

                for (int j = 2; j < 4; j++)
                    aa << std::hex << (int)buf(j);

                unsigned result2;
                aa >> result2;
                int Number2 = result2;
                cout << Number2 << "n";
            }

This gives me in the console the desired number 20000 and 10000. Now how do i do this without writing for every time i want to do this, this whole part:

for (int j = 2; j < 4; j++)
                    aa << std::hex << (int)buf(j);

                unsigned result2;
                aa >> result2;
                int Number2 = result2;
                cout << Number2 << "n";

My brain is now fried after getting to whis point from zero knowledge. Would appreciate it if someone could help me out.

optimization – Converting 4 variable if else condition to Linear integer program

There are four variables: x1, x2, x3, x4.

If you choose either x3 or x4 or both – then you should chose exactly one of x1 or x2.
If you choose neither x3 or x4 – then there is no restriction in ch0osing x1 or x2.

I have come up with the following if else logic for this, but cannot proceed from there.

enter image description here

Can you let me know how to come up with a integer linear program with this understanding?

combinatorics – Probability of forming an odd integer from a 5-permutation of integers in [0,9]

My intuition is telling me that the probability of forming an odd integer from a 5-element partial permutation of integers in (0,9) is one-half, because it should be the same probability as the integer being even, which makes those probabilities one-half.

However, I’m having trouble proving it.

I split the problem into five disjoint cases: the number consists of 1, 2, 3, 4, and 5 odd integers.

P(the number is odd and it uses only 1 odd integer) = $frac{5*4*3*2*binom{5}{1}}{10*9*8*7*6} = frac{600}{30240}$, because the last digit must be an odd integer, and there are five candidates, therefore 5 choose 1, and the rest are even integers where the first even integer has 5 candidates, the second, 4 candidates and so on.

P(the number is odd and it uses only 2 odd integer) = $frac{5*4*3*binom{4}{1}binom{5}{1}}{10*9*8*7*6} = frac{1200}{30240}$, because the last digit must be an odd integer, and there are five candidates, therefore 5 choose 1, and there is another odd integer, which has 4 possible candidates; the rest are even integers where the first even integer has 5 candidates, the second, 4 candidates and so on.

I see a problem with this approach is that I can continue to the case of 3, 4, 5, but the numerator would not add up to half of 30240. In fact, the numerator only adds up to 3720.

I feel it might be the placement of the odd integers other than the last, so I tried:

P(the number is odd and it uses only 2 odd integer) = $frac{5*4*3*4*binom{4}{1}binom{5}{1}}{10*9*8*7*6} = frac{4800}{30240}$, because the last digit must be an odd integer, and there are five candidates, therefore 5 choose 1, and there is another odd integer, which has 4 possible candidates but it can be in any of the 4 places, therefore multiplied by 4; the rest are even integers where the first even integer has 5 candidates, the second, 4 candidates and so on.

But this time the numbers are too large, and it’s way over half of 30240.

What am I doing wrong here?

unity – Why my check boolean integer not working?

so I want to add some mechanics when on my inventory, whenever the item index in this case index of scriptableobject and drop area index match the item can drop on that area

for that, I’ve actually checked like this

 if ((DragAndDropItem.draggedItem.itemDesc.indexAyat == _CheckInvent.instance.currIndex))
        {
            ...
        }

when the index of Scriptableobject match with index of drop area then do the thing, but it’s not working I don’t know why

Here the full script

public void OnDrop(PointerEventData data)
    {
        if (DragAndDropItem.icon != null)
        {
            DragAndDropItem item = DragAndDropItem.draggedItem;
            DragAndDropCell sourceCell = DragAndDropItem.sourceCell;
            if (DragAndDropItem.icon.activeSelf == true)                    // If icon inactive do not need to drop item into cell
            {
                if ((item != null) && (sourceCell != this))
                {
                    DropEventDescriptor desc = new DropEventDescriptor();
                    switch (cellType)                                       // Check this cell's type
                    {
                        case CellType.Swap:                                 // Item in destination cell can be swapped
                            UpdateMyItem();
                            switch (sourceCell.cellType)
                            {
                                case CellType.Swap:                         // Item in source cell can be swapped
                                    // Fill event descriptor
                                    desc.item = item;
                                    desc.sourceCell = sourceCell;
                                    desc.destinationCell = this;
                                    SendRequest(desc);                      // Send drop request
                                    StartCoroutine(NotifyOnDragEnd(desc));  // Send notification after drop will be finished
                                    if (desc.permission == true)            // If drop permitted by application
                                    {
                                        if (myDadItem != null)            // If destination cell has item
                                        {
                                            // Fill event descriptor
                                            DropEventDescriptor descAutoswap = new DropEventDescriptor();
                                            descAutoswap.item = myDadItem;
                                            descAutoswap.sourceCell = this;
                                            descAutoswap.destinationCell = sourceCell;
                                            SendRequest(descAutoswap);                      // Send drop request
                                            StartCoroutine(NotifyOnDragEnd(descAutoswap));  // Send notification after drop will be finished
                                            if (descAutoswap.permission == true)            // If drop permitted by application
                                            {
                                                SwapItems(sourceCell, this);                // Swap items between cells
                                            }
                                            else
                                            {
                                                PlaceItem(item);            // Delete old item and place dropped item into this cell
                                            }
                                        }
                                        else
                                        {
                                            PlaceItem(item);                // Place dropped item into this empty cell
                                        }
                                    }
                                    break;
                                default:                                    // Item in source cell can not be swapped
                                    // Fill event descriptor
                                    desc.item = item;
                                    desc.sourceCell = sourceCell;
                                    desc.destinationCell = this;
                                    SendRequest(desc);                      // Send drop request
                                    StartCoroutine(NotifyOnDragEnd(desc));  // Send notification after drop will be finished
                                    if (desc.permission == true)            // If drop permitted by application
                                    {
                                        PlaceItem(item);                    // Place dropped item into this cell
                                    }
                                    break;
                            }
                            break;
                        case CellType.DropOnly:                             // Item only can be dropped into destination cell
                                                                            // Fill event descriptor

                            desc.item = item;
                            desc.sourceCell = sourceCell;
                            desc.destinationCell = this;
                            SendRequest(desc);                              // Send drop request
                            StartCoroutine(NotifyOnDragEnd(desc));          // Send notification after drop will be finished
                            if (desc.permission == true)                    // If drop permitted by application
                            {
                                PlaceItem(item);                            // Place dropped item in this cell
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            if (item != null)
            {
                if (item.GetComponentInParent<DragAndDropCell>() == null)   // If item have no cell after drop
                {
                    Destroy(item.gameObject);                               // Destroy it
                }
            }
            UpdateMyItem();
            UpdateBackgroundState();
            sourceCell.UpdateMyItem();
            sourceCell.UpdateBackgroundState();
        }
    }

    /// <summary>
    /// Put item into this cell.
    /// </summary>
    /// <param name="item">Item.</param>
    private void PlaceItem(DragAndDropItem item)
    {
        if ((DragAndDropItem.draggedItem.itemDesc.indexAyat == _CheckInvent.instance.currIndex))
        {
            if (item != null)
            {
                DestroyItem();                                              // Remove current item from this cell
                myDadItem = null;
                DragAndDropCell cell = item.GetComponentInParent<DragAndDropCell>();
                if (cell != null)
                {
                    if (cell.unlimitedSource == true)
                    {
                        string itemName = item.name;
                        item = Instantiate(item);                               // Clone item from source cell
                        item.name = itemName;
                        //item.myImage.sprite = item.itemDesc.imageAyat;
                    }
                }
                item.transform.SetParent(transform, false);
                item.transform.localPosition = Vector3.zero;
                item.MakeRaycast(true);
                myDadItem = item;
                myDadItem.myImage.sprite = item.itemDesc.imageAyat;
                myDadItem.myImage.raycastTarget = false;
            }
            UpdateBackgroundState();
        }
    }