In the 2022 1024 card event, how do programmers judge whether their cards can form 1024? Then have fun programming it︕

In the 2022 1024 card event, how do programmers judge whether their cards can form 1024? Then have fun programming!

If you already know this activity, just skip to the code.

Similar to last year, this year’s Likou held the 1024 Programmer’s Day event.

The event address ishttps://leetcode.cn/2022-1024/

The activity guide ishttps://leetcode.cn/circle/discuss/OV9VUd/

Let me explain first, this articleNot an advertisement, because there are many programmers who are obsessed with it, and there is no need for me, a small blogger with no traffic, to promote it.

Probably the gameplay is to obtain cards through various channels

Cards are divided intodigital cardandCompliant card

Through three operations of four numbers, the result is obtained1024, you will get a “1024 coin”

Soon, I will have a lot of cards, so the question is, can I make up 1024 with so many cards?

Then have fun programming!

Just change the “numbers” and “operators” in the Python code below to your own, and then run the script.

The program will automatically calculate whether your card can get 1024 through “four counts and three counts”.

For example, my current cards include:[2, 13, 4, 26, 2, 2, 14, 18, 2]and[">>", "&", "|", "|", "%"]

Then the running result is:

[]

┭┮﹏┭┮

But if “the first successfully synthesized card of a great god in the entire network”:[16, 2, 16, 0]and["<<", "*", "+"]

Then the running result is

['(((16<<2)*16)+0)', '(((16<<2)+0)*16)', '(((16*16)<<2)+0)', '(((16*16)+0)<<2)', '(((16+0)<<2)*16)', '(((16+0)*16)<<2)', '(((16*16)<<2)+0)', '(((16*16)+0)<<2)', '(((16<<2)*16)+0)', '(((16<<2)+0)*16)', '(((16+0)*16)<<2)', '(((16+0)<<2)*16)', '(((0+16)<<2)*16)', '(((0+16)*16)<<2)', '(((0+16)*16)<<2)', '(((0+16)<<2)*16)']

There are many ways to form 1024.

Okay, enough nonsense, here’s the code:

version 1

'''
Author: LetMeFly
Date: 2022-10-19 15:41:14
LastEditors: LetMeFly
LastEditTime: 2022-10-19 18:25:27
'''

# Just change the number cards and symbol cards here to your own
numbers = [2, 13, 4, 26, 2, 2, 14, 18, 2]
operators = [">>", "&", "|", "|", "%"]


ok = []

for n1 in range(len(numbers)):
    for n2 in range(len(numbers)):
        for n3 in range(len(numbers)):
            for n4 in range(len(numbers)):
                for o1 in range(len(operators)):
                    for o2 in range(len(operators)):
                        for o3 in range(len(operators)):
                            if (n1 == n2 or n1 == n3 or n1 == n4 or n2 == n3 or n2 == n4 or n3 == n4) or (o1 == o2 or o2 == o3 or o1 == o3):
                                continue
                            string = f"((({numbers[n1]}{operators[o1]}{numbers[n2]}){operators[o2]}{numbers[n3]}){operators[o3]}{numbers[n4]})"
                            if eval(string) == 1024:
                                ok.append(string)

print(ok)

Version 2 (updated on 2022-10-20 10 52)

  1. Fixed a bug where the dividend may be 0 during modulo or division operations.
  2. Shows the number of attempts
'''
Author: LetMeFly
Date: 2022-10-19 15:41:14
LastEditors: LetMeFly
LastEditTime: 2022-10-20 10:22:52
'''

# numbers = [16, 2, 16, 0]
# operators = ["<<", "*", "+"]
numbers = [0, 12, 5, 2, 3, 19, 2, 18, 2, 13, 4, 26, 2, 2, 14]
operators = ["*", ">>", "&", "|", "|", "%"]


ok = []

print(f"{len(numbers)} ^ 4 * {len(operators)} ^ 3 = {len(numbers) ** 4 * len(operators) ** 3}")

for n1 in range(len(numbers)):
    for n2 in range(len(numbers)):
        for n3 in range(len(numbers)):
            for n4 in range(len(numbers)):
                for o1 in range(len(operators)):
                    for o2 in range(len(operators)):
                        for o3 in range(len(operators)):
                            if (n1 == n2 or n1 == n3 or n1 == n4 or n2 == n3 or n2 == n4 or n3 == n4) or (o1 == o2 or o2 == o3 or o1 == o3):
                                continue
                            string = f"((({numbers[n1]}{operators[o1]}{numbers[n2]}){operators[o2]}{numbers[n3]}){operators[o3]}{numbers[n4]})"
                            try:
                                if eval(string) == 1024:
                                    ok.append(string)
                            except:
                                pass

print(ok)

I write this article for two purposes, one is to share, and the other is to gain some followers.

I made a simple little website that can do online calculationsCount1024.LetMeFly.xyz


I made another website where you can receive and share cardsShare1024.LetMeFly.xyz

Posted simultaneously on, originality is not easy, please attach it when reprintingOriginal linkOh~
Tisfy:https://letmefly.blog.csdn.net/article/details/127413787

Related Posts

20220313 Simulation Competition Summary

TOPSIS method – comprehensive evaluation using original data

[LeetCode]Replace spaces&&disappeared numbers&&split linked list&&product of arrays except itself

Anti-crawler SSL TLS fingerprinting and bypass JA3 algorithm.md

Detailed explanation of K-means clustering algorithm + Python code example

LeetCode Greedy Algorithm 1005. Maximized array sum after K times inversion

LeetCode 884 Uncommon words in two sentences [String] HERODING’s road to LeetCode

Naive Bayes Classification

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*