アプリ版:「スタンプのみでお礼する」機能のリリースについて

実は順番あるんですか??
たとえば
linked list で
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt

てりすとを反転させるときに煩雑だから
もっとわかりやすくpythonでは以下ができて
curr.next, prev, curr = prev, curr, curr.next
てできると思いますけど、
これはこの順番だとわかりますけど、
例えば
prev, curr, curr.next = curr, curr.next, prev
とすると
NoneTypeObject の.nextを得ようとしちゃってだめですて
エラーが出ます。
これはwhile currの最後の --> Null のところのケースでなってる
気がしますけど、なんでこのすごい対称にみえるのに入れ替えたら
動かかなく成るかわかりません。

A 回答 (8件)

To be honest, the C has nothing...well, it has numbers and pointers, but that's all. That is why #143 has the definition for singly-linked list and you have to write a messy code.


You said you were afraid of the functional programming, but I will show you how to do it in Python with its built-in list. You will see the functional programming is much easier and more powerful to solve #143, so let's ignore its weird requirement.
Here's an example consisting of 5 steps as follows:

STEP1: PREPARE A LIST

ex1) lst = [1, 2, 3, 4]
ex2) lst = [1, 2, 3, 4, 5]

STEP2: GET THE REVERSED ONE

ex1) reversed(lst) => [4, 3, 2, 1]
ex2) reversed(lst) => [5, 4, 3, 2, 1]

STEP3: ZIP THEM(THE ORIGINAL LIST AND ITS REVERSED ONE)
ex1) zip(lst, reversed(lst)) => [(1, 4), (2, 3), (3, 2), (4, 1)]
ex2) zip(lst, reversed(lst)) => [(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]

STEP4: FLATTEN IT
ex1) chain.from_iterable(zip(lst, reversed(lst))) => [1, 4, 2, 3, 3, 2, 4, 1]
ex2) chain.from_iterable(zip(lst, reversed(lst))) => [1, 5, 2, 4, 3, 3, 4, 2, 5, 1]

itertools.chain.from_iterable:
https://docs.python.org/3/library/itertools.html …

STEP5: SLICE IT
ex1: list(chain.from_iterable(zip(lst, reversed(lst))))[:len(lst)] => [1, 4, 2, 3]
ex2: list(chain.from_iterable(zip(lst, reversed(lst))))[:len(lst)] => [1, 5, 2, 4, 3]

That's all. No destruction, but you get what you want.
Finally the code is something like this:

Reorder List in the Functional Programming:
https://www.ideone.com/Y2z3fq

As a result, you can write the logic in one line(because, without its weird requirement, it is just simple) with the functional programming way. In other words, the C-style programming would be much more complex and messy.
But what about its speed? I ain't known whether it is fast or not. It might waste CPU time, but it does not waste OUR time, because the source code above is easier and simpler to write; thus it does not take a long time to make.
More than 30 years ago, CPU's were slow and expensive. RAMs were small and limited. In those times, hardware was much more valuable than programmers were.
Today, situation is drastically changed. We ARE much more than valuable than hardware is. CPU becomes faster and cheaper. RAMs become so huge. A lot of things processed on the computers are not so critical. We do not have to pay attention to computers' calculation speed so much(except video games as you mentioned, and maybe browsers).
I have said it once but I'll say it again. DO NOT PAY ATTENTION TO THE SPEED TOO MUCH.
I do not know whether you check it out or not, but do you remember the 7th on your question about optimization? He insisted that his code was fast on his profiler, but in reality, his code did not work properly. He wrote a wrong code and believed his own code ran fast.
Do you remember I said "to optimize a code too much makes bugs easily"? He definitely demonstrated that. He too much concentrated on staring at his monitor, or numbers the profiler show; as a result, he forgot what is the right code for it.
Blame him? No. That can be happened to me, you, and everyone. We all concentrate on programming that it is very easy to narrow of our visual field. To avoid that... do not be maniac for speed.
Anyway, competitive programming does not necessarily educate you about good programming habits. competitive programming is heavily rely on the C style, and it is not for the other languages. In addition to that, the C's way of programming is what you MUST NOT do in a lot of programming languages. Otherwise, study the C and use it if you are really serious at competitive programming.
Period.
    • good
    • 0

> こんな感じでだいじょうぶですか??



Yeah, that is more accurate way to post here. On Oshiete goo, indentation is killed; thus using Ideone avoid annoying.
Besides, you had better what you try writing code every time.

> 実は順番あるんですか??

Yes. Please refer to this:

7.2. Assignment statements:
https://docs.python.org/3/reference/simple_stmts …

"The object must be an iterable(※ in this case, tuple) with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets."

"from left to right". This means the assignment does not occur simultaneously. The assignment occurs in a certain order.

※: In Python, the way of assignment:

prev, curr, curr.next = curr, curr.next, prev

is processed as tuple:

(prev, curr, curr.next) = (curr, curr.next, prev)

Well, LeetCode again? I guess this question might come from here:

143. Reorder List:
https://leetcode.com/problems/reorder-list/descr …

I have to say this problem sucks. A piece of SHIT. Totally FUCKED UP.
Let me explain why I think so, but the first thing is the first. You have to know concepts in Computer Science.
There are two ways to group processes in programming. The one is function, returning a value(Some languages allow returning values) as you know. The other is called procedure, where its purpose is side-effects.
Let's think a simple example. Writing a function to add 1 and 2 can be like this:

def foo():
 return 1 + 2

This is an ordinal way as you know. On the other hand, a procedure is written like this:

val = None
def bar():
 global val
 val = 1 + 2

You will find the variable val is DESTRUCTIVELY changed to 3 after running the procedure bar. Destructive update is one of side-effects.
Of course, the second one, procedural way, is not expected in the modern approach, generally speaking. Thus, some modern programming languages use immutable data structures to avoid this kind of destructive update.
However, the problem have such a weird restriction:

"Do not return anything, modify head in-place instead."

This is so fuckin' weird. An Unnecessary requirement for Python. Why?
I will tell you a truth and its background. What you have to know is there is NO UNIVERSAL PROBLEM ACROSS ANY PROGRAMMING LANGUAGES.
Strange? But this is true. I know LeetCode allow using C++, Java, C#, and blah-blah-blah...but, this is a sort of FAKE. But why again?
You have to know a programming language is a tool to think, and this means different languages make users think in the different ways. THIS IS THE TRUTH. Therefore, there is NO universal way of thinking on every programming language.
And here is the background. Frankly speaking, problems in competitive programming are made in C, or C-likes. In other words, questioners think in and assume the C language, for instance.
However, all programming languages are not same and powerful, consequently questioners force people to be chained and in bondage.
This makes your way of programming in the god-damn bad manner. Using Python in the C lang.? NO FUCKIN' KIDDING.
So be cautious. DO NOT BE SO SERIOUS at LeetCode, nor competitive programming. This makes you a bad Python programmer. In addition to that, this kind of site DOES NOT show you algorithms, which are REALLY universal ways of thinking, relying on mathematics.
I think RosettaCode is much much better in order to learn such algorithms, which show each typical way for each programming language.
    • good
    • 0
この回答へのお礼

つらい・・・

ほんとに好きです。。

お礼日時:2024/04/17 19:28

Ruby なのか Python なのか (はたまたどちらでもないなにかなのか), どれなんだろうねぇ.



Ruby についてはちょっと調べた限りだと「左から右に評価して左から右に代入」だと思われる>#5. Python の場合は代入に限り「右辺→左辺」の順に評価 (それぞれの中では左→右に評価) して左から右に代入の模様.

たぶん副作用は評価と同時に適用されるんじゃないかなぁ.

あぁ, Ruby だと 3.1 で評価順序に手が入ってるから注意ね.
    • good
    • 1

←No.4「お礼欄」


私も検索してみましたが、確かに説明を見かけないですね。
Ruby の多重代入について説明しているサイトは多いのですが、
評価順や展開されるコードについての説明はどこにもありません。
Ruby の公式サイトにすら、多重代入をリストに使ったらどうなるか
の説明はくどいほど書いてあるのに、左辺や右辺の各項目の評価順
については、何も書いてありませんでした。これは、問題だと思います。
https://docs.ruby-lang.org/ja/latest/doc/spec=2f …

あまりにラチがあかないので、今日、本屋でまつもとひとし氏本人の
著書を立ち読みしてきましたが、やはり評価順については何も
書いてありませんでした。その本(https://www.amazon.co.jp/%E3%82%AA%E3%83%96%E3%8 …に、
a,b = b,a で変数の値の入れ替えができると明記してあったので、
No.4 は間違いなのかもしれません。

C や Java の世代の私にとっては、信じられないような状況です。
今どき(Ruby は既に「今どき」じゃないか...)のドキュメンとって、
この程度のものなんでしょうか?
それとも、ここまで読まにゃならんって話なのかな?
https://book.impress.co.jp/books/1721
こういう言語は、正直カンベンです。

多重代入の右辺に副作用のあるメソッド呼び出しが書かれたら
何が起こるのか?全く謎です。 手元の処理系で実験しても、
仕様なのか、処理系依存なのか、区別する方法がありません。

多重代入の項の並び順に意味がなく、同時に代入される
つまり、 curr.next, prev, curr = prev, curr, curr.next が
temp1 = prev
temp2 = curr
temp3 = curr.next
curr.next = temp1,
prev = tamp2
curr = temp3
の意味で解釈されるとしても、
curr.next = temp1 と curr = temp3 の実行順には意味があるはずです。
それによって、curr.next = temp1 が実行されるときの curr の値が
違ってしまうからです。

多重代入の左辺と右辺の評価順については、このような↓サイトを見かけました。
https://qiita.com/ailfghdsiau/items/728cc565c82c …
左辺内,右辺内での評価順に関する記述は、ネットにも本にも
見つけられませんでした。Orz.
    • good
    • 0

prev, curr, curr.next = curr, curr.next, prev


は、3個同時に代入するんじゃなくて、
左から順に
prev = curr
curr = curr.next
curr.next = prev
と実行するんでしょ。

while curr: を通過した時点で curr の中身が Null じゃなくても
curr.next の中身が Null だったら、
curr = curr.next したときに curr の中身が Null になるから
次の curr.next = prev が Null.next への代入になっちゃう。
これは while ループがリストの末尾にきたとき起こる。
    • good
    • 0
この回答へのお礼

助かりました

にゃるほど。。。そうだとおおもいましたけど、どこにもそのような説明がないので、ありものがたりさんはどうやてわかりましたか????

お礼日時:2024/04/13 21:21

> NoneTypeObject の.nextを得ようとしちゃってだめです


じゃなくて、
「NoneTypeObject の.nextに代入しようとしちゃってだめです」
でしょうね。
    • good
    • 0
この回答へのお礼

助かりました

お礼日時:2024/04/13 21:21

n = curr.next


p = prev
c = curr

curr.next = p # c.next = p
prev = c
curr = n

が元のプログラム

p = prev
c = curr
n = curr.next

prev = c
curr = n
curr.next = p # n.next = p

が変更したプログラムと同等のコードになります。
    • good
    • 0
この回答へのお礼

助かりました

お礼日時:2024/04/13 21:21

いや、こういう質問をする場合は貴女が書いたソースコードを提示せんと何が起きたんだかサッパリ分からないよ。


ideoneにでも貼っつけて、こっちにリンクを貼った方がいいよ。

ideone:
https://www.ideone.com/

(そもそも、リストがあるPythonに於いて、何故に自分で連結リストを実装せなアカンのか意味が分からんし・・・・・・)
    • good
    • 0
この回答へのお礼

ありがとう

えと、ごめんなさい。こんな感じでだいじょうぶですか??
なんかほんとは入力がされるからそれは出来ませんでした。
たしかに、リストの実装をじぶんでするのはおもしろいです笑

https://www.ideone.com/QQLlX6

お礼日時:2024/04/12 00:02

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています


このQ&Aを見た人がよく見るQ&A