Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Single tasking: Just Say No.


devel / comp.lang.python / Re: Packing Problem

SubjectAuthor
* Re: Packing ProblemRob Cliffe
`* Re: Packing ProblemHen Hanna
 `* Re: Packing ProblemHen Hanna
  `- Re: Packing ProblemHen Hanna

1
Re: Packing Problem

<mailman.2125.1677784217.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22141&group=comp.lang.python#22141

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rob.cli...@btinternet.com (Rob Cliffe)
Newsgroups: comp.lang.python
Subject: Re: Packing Problem
Date: Thu, 2 Mar 2023 19:09:47 +0000
Lines: 75
Message-ID: <mailman.2125.1677784217.20444.python-list@python.org>
References: <d9a7e11f-abc5-6519-4e4a-f74435aa8d8e@btinternet.com>
<6e36f542-e4b6-35f8-5530-1115360de252@btinternet.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 54jv0PDQ8bPLYZ2ZIFXReAyl0unNuIjD4qdzSjJKKnIg==
Return-Path: <rob.cliffe@btinternet.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=btinternet.com header.i=@btinternet.com header.b=B2Mc2Ubr;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.175
X-Spam-Level: *
X-Spam-Evidence: '*H*': 0.67; '*S*': 0.02; 'def': 0.04; 'skip:\xc2
30': 0.07; 'answer:': 0.09; 'shortest': 0.09; 'skip:\xc2 20':
0.09; 'meanwhile': 0.16; 'stuck': 0.16; 'instead': 0.17; 'to:addr
:python-list': 0.20; 'version': 0.23; 'else': 0.27; 'header:User-
Agent:1': 0.30; 'forward,': 0.32; 'received:192.168.1': 0.32;
'but': 0.32; 'there': 0.33; 'header:In-Reply-To:1': 0.34; 'words':
0.35; 'final': 0.35; 'received:192.168': 0.37; '8bit%:14': 0.38;
'adding': 0.39; 'safe': 0.39; 'break': 0.39; 'beginning': 0.40;
'received:213': 0.40; 'try': 0.40; 'initial': 0.61; 'method':
0.61; 'skip:o 10': 0.61; 'skip:\xc2 10': 0.62; 'gives': 0.62;
'true': 0.63; 'improved': 0.64; '8bit%:96': 0.69; 'end,': 0.69;
'8bit%:100': 0.76; 'characters': 0.84; 'rob': 0.84; 'true:': 0.84;
'words:': 0.84; 'dependent': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=btinternet.com;
s=btmx201904; t=1677784210;
bh=WLJXDUaXvSBg+7KyQ9rRYNTBg3IXdEi8JDWVR5Jve0I=;
h=Message-ID:Date:MIME-Version:Subject:To:References:From:In-Reply-To;
b=B2Mc2UbrUDTxBlXq7J3IhbZrf8Al0hYOzGxnwf2xNiPN34xfkvYKSEBhnjXerxCkEABu8xJz39B8SGYHKNT8ZcrqlC7kaB28MBu6sdOcO0CKjUIcC76qLKfD//dGZ4vylFvtqGtjUX0yfttXI/95N6bBJSSJq8dkOeQMAxhNiU/fhoxZB/pzAip09rBhXHgwNN9I3yO1OSYayFqw39bJRcHxtcK21VPHJRDANv4sGzu2vEsvQpcWAKsvshT/hqAyeCDyD0qgsva4TkSiCmlrupAdRCdsj9FOtBf7SgFO9Cl7M4LLLhrq2ervYnUhqqT2yV31uoDUrpGjv0K2r/HvLA==
Authentication-Results: btinternet.com;
auth=pass (PLAIN) smtp.auth=rob.cliffe@btinternet.com;
bimi=skipped
X-SNCR-Rigid: 6139429051A0E3E8
X-Originating-IP: [86.130.30.128]
X-OWM-Source-IP: 86.130.30.128 (GB)
X-OWM-Env-Sender: rob.cliffe@btinternet.com
X-VadeSecure-score: verdict=clean score=0/300, class=clean
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvhedrudeljedguddulecutefuodetggdotefrodftvfcurfhrohhfihhlvgemuceutffkvffkuffjvffgnffgvefqofdpqfgfvfenuceurghilhhouhhtmecufedtudenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtfeejnecuhfhrohhmpeftohgsucevlhhifhhfvgcuoehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmqeenucggtffrrghtthgvrhhnpedvteduhfetgfevgeeiffelledvuefgieekveevhedutefgveeijeeftdetvedtgeenucfkphepkeeirddufedtrdeftddruddvkeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhephhgvlhhopegludelvddrudeikedruddrvddtgegnpdhinhgvthepkeeirddufedtrdeftddruddvkedpmhgrihhlfhhrohhmpehrohgsrdgtlhhifhhfvgessghtihhnthgvrhhnvghtrdgtohhmpdhnsggprhgtphhtthhopedupdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrdhorhhg
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-SNCR-hdrdom: btinternet.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.8.0
Content-Language: en-GB
In-Reply-To: <d9a7e11f-abc5-6519-4e4a-f74435aa8d8e@btinternet.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <6e36f542-e4b6-35f8-5530-1115360de252@btinternet.com>
X-Mailman-Original-References: <d9a7e11f-abc5-6519-4e4a-f74435aa8d8e@btinternet.com>
 by: Rob Cliffe - Thu, 2 Mar 2023 19:09 UTC

Slightly improved version (deals with multiple characters together
instead of one at a time):

# Pack.py
def Pack(Words):
    if not Words:
        return ''
    # The method is to build up the result by adding letters at the
beginning
    # and working forward, and by adding letters at the end, working
backwards,
    # meanwhile shrinking the words in the list.
    Words = list(Words) # Don't mutate the original
    Initial = ''
    Final = ''
    while True:
        # It is safe to add an initial letter (of one or more of the
words) if
        # EITHER    There is no word that contains it as
        #             a non-initial letter but not as an initial letter.
        #  OR       All words start with it.
        while True:
            FirstLetters = set(w[0] for w in Words)
            FirstLettersSafe = sorted(ch for ch in FirstLetters if
                all(w[0]==ch for w in Words)
                or not any(ch in w[1:] and w[0]!=ch for w in Words))
                # sorted() to make the answer deterministic
                # (not dependent on set ordering)
            if not FirstLettersSafe:
                break
            AnyProgress = True
            Initial += ''.join(FirstLettersSafe)   # Build up the
answer from the beginning
            Words = [ (w[1:] if w[0] in FirstLettersSafe else w) for w
in Words ]
            Words = [ w for w in Words if w != '']
            if not Words:
                return Initial + Final
        # It is safe to add a final letter (of one or more of the words) of
        # EITHER    There is no word that contains it as
        #             a non-final letter but not as a final letter.
        #  OR       All words end with it.
        while True:
            LastLetters = set(w[-1] for w in Words)
            LastLettersSafe = sorted(ch for ch in LastLetters if
                all(w[-1]==ch for w in Words)
                or not any(ch in w[:-1] and w[-1]!=ch for w in Words))
                # sorted() to make the answer deterministic
                # (not dependent on set ordering)
            if not LastLettersSafe:
                break
            Final = ''.join(LastLettersSafe) + Final   # Build up the
answer from the end
            Words = [ (w[:-1] if w[-1] in LastLettersSafe else w) for w
in Words ]
            Words = [ w for w in Words if w != '']
            if not Words:
                return Initial + Final
        if not (FirstLettersSafe or LastLettersSafe):
            break # stuck
    # Try all the possibilities for the next letter to add at the
beginning,
    # with recursive calls, and pick one that gives a shortest answer:
    BestResult = None
    for ch in FirstLetters:
            Words2 = list( (w[1:] if w[0] == ch else w) for w in Words )
            Words2 = [ w for w in Words2 if w != '' ]
            res = ch + Pack(Words2)
            if BestResult is None or len(res) < len(BestResult):
                BestResult = res
    return Initial + BestResult + Final

print(Pack(['APPLE', 'PIE', 'APRICOT', 'BANANA', 'CANDY']))

Rob Cliffe

Re: Packing Problem

<cd924c9d-2141-4aae-bd84-a3271c755d9bn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22175&group=comp.lang.python#22175

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a05:6214:a8d:b0:56e:9499:c877 with SMTP id ev13-20020a0562140a8d00b0056e9499c877mr768618qvb.2.1677873589886;
Fri, 03 Mar 2023 11:59:49 -0800 (PST)
X-Received: by 2002:a5b:2cc:0:b0:a02:a3a6:78fa with SMTP id
h12-20020a5b02cc000000b00a02a3a678famr1367031ybp.12.1677873589575; Fri, 03
Mar 2023 11:59:49 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Fri, 3 Mar 2023 11:59:49 -0800 (PST)
In-Reply-To: <mailman.2125.1677784217.20444.python-list@python.org>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:648:8600:a230:880a:247b:66ed:12f6;
posting-account=YjTkGAoAAAA4_fbAISfvtIqrYbghMeBx
NNTP-Posting-Host: 2601:648:8600:a230:880a:247b:66ed:12f6
References: <6e36f542-e4b6-35f8-5530-1115360de252@btinternet.com>
<d9a7e11f-abc5-6519-4e4a-f74435aa8d8e@btinternet.com> <mailman.2125.1677784217.20444.python-list@python.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <cd924c9d-2141-4aae-bd84-a3271c755d9bn@googlegroups.com>
Subject: Re: Packing Problem
From: henha...@gmail.com (Hen Hanna)
Injection-Date: Fri, 03 Mar 2023 19:59:49 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 4890
 by: Hen Hanna - Fri, 3 Mar 2023 19:59 UTC

On Thursday, March 2, 2023 at 11:10:37 AM UTC-8, Rob Cliffe wrote:
> Slightly improved version (deals with multiple characters together
> instead of one at a time):
> # Pack.py
> def Pack(Words):
> if not Words:
> return ''
> # The method is to build up the result by adding letters at the
> beginning
> # and working forward, and by adding letters at the end, working
> backwards,
> # meanwhile shrinking the words in the list.
> Words = list(Words) # Don't mutate the original
> Initial = ''
> Final = ''
> while True:
> # It is safe to add an initial letter (of one or more of the
> words) if
> # EITHER There is no word that contains it as
> # a non-initial letter but not as an initial letter.
> # OR All words start with it.
> while True:
> FirstLetters = set(w[0] for w in Words)
> FirstLettersSafe = sorted(ch for ch in FirstLetters if
> all(w[0]==ch for w in Words)
> or not any(ch in w[1:] and w[0]!=ch for w in Words))
> # sorted() to make the answer deterministic
> # (not dependent on set ordering)
> if not FirstLettersSafe:
> break
> AnyProgress = True
> Initial += ''.join(FirstLettersSafe) # Build up the
> answer from the beginning
> Words = [ (w[1:] if w[0] in FirstLettersSafe else w) for w
> in Words ]
> Words = [ w for w in Words if w != '']
> if not Words:
> return Initial + Final
> # It is safe to add a final letter (of one or more of the words) of
> # EITHER There is no word that contains it as
> # a non-final letter but not as a final letter.
> # OR All words end with it.
> while True:
> LastLetters = set(w[-1] for w in Words)
> LastLettersSafe = sorted(ch for ch in LastLetters if
> all(w[-1]==ch for w in Words)
> or not any(ch in w[:-1] and w[-1]!=ch for w in Words))
> # sorted() to make the answer deterministic
> # (not dependent on set ordering)
> if not LastLettersSafe:
> break
> Final = ''.join(LastLettersSafe) + Final # Build up the
> answer from the end
> Words = [ (w[:-1] if w[-1] in LastLettersSafe else w) for w
> in Words ]
> Words = [ w for w in Words if w != '']
> if not Words:
> return Initial + Final
> if not (FirstLettersSafe or LastLettersSafe):
> break # stuck
> # Try all the possibilities for the next letter to add at the
> beginning,
> # with recursive calls, and pick one that gives a shortest answer:
> BestResult = None
> for ch in FirstLetters:
> Words2 = list( (w[1:] if w[0] == ch else w) for w in Words )
> Words2 = [ w for w in Words2 if w != '' ]
> res = ch + Pack(Words2)
> if BestResult is None or len(res) < len(BestResult):
> BestResult = res
> return Initial + BestResult + Final
>
> print(Pack(['APPLE', 'PIE', 'APRICOT', 'BANANA', 'CANDY']))
> Rob Cliffe

thanks.... i'd be surprised to find a shorter answer.

bappricnanadloety =17 chars

Re: Packing Problem

<69cef51e-21c7-4a7c-835f-25ea716580e8n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22179&group=comp.lang.python#22179

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ac8:40c9:0:b0:3bd:17ec:5fc9 with SMTP id f9-20020ac840c9000000b003bd17ec5fc9mr1144899qtm.6.1677893645775;
Fri, 03 Mar 2023 17:34:05 -0800 (PST)
X-Received: by 2002:a81:b612:0:b0:52e:ec03:9b2f with SMTP id
u18-20020a81b612000000b0052eec039b2fmr2221167ywh.8.1677893645553; Fri, 03 Mar
2023 17:34:05 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Fri, 3 Mar 2023 17:34:05 -0800 (PST)
In-Reply-To: <cd924c9d-2141-4aae-bd84-a3271c755d9bn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:648:8600:a230:880a:247b:66ed:12f6;
posting-account=YjTkGAoAAAA4_fbAISfvtIqrYbghMeBx
NNTP-Posting-Host: 2601:648:8600:a230:880a:247b:66ed:12f6
References: <6e36f542-e4b6-35f8-5530-1115360de252@btinternet.com>
<d9a7e11f-abc5-6519-4e4a-f74435aa8d8e@btinternet.com> <mailman.2125.1677784217.20444.python-list@python.org>
<cd924c9d-2141-4aae-bd84-a3271c755d9bn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <69cef51e-21c7-4a7c-835f-25ea716580e8n@googlegroups.com>
Subject: Re: Packing Problem
From: henha...@gmail.com (Hen Hanna)
Injection-Date: Sat, 04 Mar 2023 01:34:05 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2755
 by: Hen Hanna - Sat, 4 Mar 2023 01:34 UTC

On Friday, March 3, 2023 at 12:00:00 PM UTC-8, Hen Hanna wrote:
> On Thursday, March 2, 2023 at 11:10:37 AM UTC-8, Rob Cliffe wrote:
> > Slightly improved version (deals with multiple characters together instead of one at a time):

> > # Pack.py

> > def Pack(Words):
> > if not Words:
> > return ''
> > # The method is to build up the result by adding letters at the beginning
> > # and working forward, and by adding letters at the end, working backwards,
> > # meanwhile shrinking the words in the list.

> > Words = list(Words) # Don't mutate the original
> > Initial = ''
> > Final = ''
> > while True:

> > # It is safe to add an initial letter (of one or more of the words) if
> > # EITHER There is no word that contains it as
> > # a non-initial letter but not as an initial letter.
> > # OR All words start with it.

is ths (above) the same as this?

It is safe to add an initial letter of (subset of) the words if
All words start with it.
OR ELSE
The remaining words don't contain that letter.

> thanks.... i'd be surprised to find a shorter answer.
>
>
> bappricnanadloety =17 chars

Re: Packing Problem

<a86e71c2-992f-4066-aa8d-c06710e6095fn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=22193&group=comp.lang.python#22193

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ac8:6101:0:b0:3b7:fda4:c86e with SMTP id a1-20020ac86101000000b003b7fda4c86emr1507430qtm.3.1677955684725;
Sat, 04 Mar 2023 10:48:04 -0800 (PST)
X-Received: by 2002:a25:9cc7:0:b0:a28:737a:b933 with SMTP id
z7-20020a259cc7000000b00a28737ab933mr3372967ybo.9.1677955684491; Sat, 04 Mar
2023 10:48:04 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Sat, 4 Mar 2023 10:48:04 -0800 (PST)
In-Reply-To: <69cef51e-21c7-4a7c-835f-25ea716580e8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:648:8600:b650:58b9:f597:ffc9:a070;
posting-account=YjTkGAoAAAA4_fbAISfvtIqrYbghMeBx
NNTP-Posting-Host: 2601:648:8600:b650:58b9:f597:ffc9:a070
References: <6e36f542-e4b6-35f8-5530-1115360de252@btinternet.com>
<d9a7e11f-abc5-6519-4e4a-f74435aa8d8e@btinternet.com> <mailman.2125.1677784217.20444.python-list@python.org>
<cd924c9d-2141-4aae-bd84-a3271c755d9bn@googlegroups.com> <69cef51e-21c7-4a7c-835f-25ea716580e8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a86e71c2-992f-4066-aa8d-c06710e6095fn@googlegroups.com>
Subject: Re: Packing Problem
From: henha...@gmail.com (Hen Hanna)
Injection-Date: Sat, 04 Mar 2023 18:48:04 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Hen Hanna - Sat, 4 Mar 2023 18:48 UTC

On Friday, March 3, 2023 at 5:34:14 PM UTC-8, Hen Hanna wrote:
> On Friday, March 3, 2023 at 12:00:00 PM UTC-8, Hen Hanna wrote:
> > On Thursday, March 2, 2023 at 11:10:37 AM UTC-8, Rob Cliffe wrote:
> > > Slightly improved version (deals with multiple characters together instead of one at a time):
>
> > > # Pack.py
>
> > > def Pack(Words):
> > > if not Words:
> > > return ''
> > > # The method is to build up the result by adding letters at the beginning
> > > # and working forward, and by adding letters at the end, working backwards,
> > > # meanwhile shrinking the words in the list.
>
> > > Words = list(Words) # Don't mutate the original
> > > Initial = ''
> > > Final = ''
> > > while True:
>
> > > # It is safe to add an initial letter (of one or more of the words) if
> > > # EITHER There is no word that contains it as
> > > # a non-initial letter but not as an initial letter.
> > > # OR All words start with it.
> is ths (above) the same as this?
>
>
> It is safe to add an initial letter of (subset of) the words if
> All words start with it.
> OR ELSE
> The remaining words don't contain that letter.
> > thanks.... i'd be surprised to find a shorter answer.
> >
> >
> > bappricnanadloety =17 chars

when i Copy-Pasted the code (into a file) and tried run it, i got:

> py packBad.py
SyntaxError: Non-UTF-8 code starting with '\xa0' in file C:\pypy3.9............\packBad.py on line 3, but no encoding declared.........

and so i needed to put this ( # -*- coding: UTF-8 -*- ) to fix the problem..

_________________________

'\xa0' is ....

\xa0 is actually non-breaking space in Latin1 (ISO 8859-1), also chr(160). You should replace it with a space

Where is it in line-3 ? ----- where line 3 is
# Slightly improved version (deals with multiple characters together instead of one at a time):

>>> To remove xa0 from string in Python, we can use the value NFKD in the normalize() function, which is an abbreviation for Normal Form KD . The use of NFKD in the normalize() function results in the substitution of all the characters into their equivalent values. The equivalent value of xa0, for example, is a space.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor