実は順番あるんですか??
たとえば
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 のところのケースでなってる
気がしますけど、なんでこのすごい対称にみえるのに入れ替えたら
動かかなく成るかわかりません。
No.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.
No.7
- 回答日時:
> こんな感じでだいじょうぶですか??
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.
No.6
- 回答日時:
Ruby なのか Python なのか (はたまたどちらでもないなにかなのか), どれなんだろうねぇ.
Ruby についてはちょっと調べた限りだと「左から右に評価して左から右に代入」だと思われる>#5. Python の場合は代入に限り「右辺→左辺」の順に評価 (それぞれの中では左→右に評価) して左から右に代入の模様.
たぶん副作用は評価と同時に適用されるんじゃないかなぁ.
あぁ, Ruby だと 3.1 で評価順序に手が入ってるから注意ね.
No.5
- 回答日時:
←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.
No.4
- 回答日時:
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 ループがリストの末尾にきたとき起こる。
にゃるほど。。。そうだとおおもいましたけど、どこにもそのような説明がないので、ありものがたりさんはどうやてわかりましたか????
No.1
- 回答日時:
いや、こういう質問をする場合は貴女が書いたソースコードを提示せんと何が起きたんだかサッパリ分からないよ。
ideoneにでも貼っつけて、こっちにリンクを貼った方がいいよ。
ideone:
https://www.ideone.com/
(そもそも、リストがあるPythonに於いて、何故に自分で連結リストを実装せなアカンのか意味が分からんし・・・・・・)
えと、ごめんなさい。こんな感じでだいじょうぶですか??
なんかほんとは入力がされるからそれは出来ませんでした。
たしかに、リストの実装をじぶんでするのはおもしろいです笑
https://www.ideone.com/QQLlX6
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Visual Basic(VBA) 順列をランダムに発生するプログラム 1 2022/11/16 12:16
- C言語・C++・C# c言語の問題です 3 2023/01/10 16:15
- Visual Basic(VBA) 前回ご教授いただいたコードに覚えたてのループ処理で品名りんごAから順に20回for nextでループ 7 2023/01/13 22:01
- 大学・短大 C言語線形リストの問題です 3 2022/12/22 00:45
- その他(動画サービス) U-NEXTとNETFLIX 2 2023/09/03 14:30
- JavaScript jQuery . プログラミング 助けて下さい 変数「$displayslide」からactiveク 2 2023/09/08 23:07
- その他(動画サービス) 至急!携帯でプランに入ってるU-NEXTをテレビに繋げたいのですが手順がわかりません。Fire TV 1 2022/09/02 01:40
- その他(動画サービス) 動画配信サービスのAmazon Prime と U-NEXTとの比較について 4 2023/04/15 11:31
- その他(動画サービス) U-NEXTについて教えて下さい。 VIVANT全話を視聴したいので、U-NEXTの31日間無料トラ 1 2023/09/17 18:51
- Visual Basic(VBA) xmlドキュメントから別拡張子で保存したい 4 2023/09/12 11:08
このQ&Aを見た人はこんなQ&Aも見ています
-
性格の違いは生まれた順番で決まる?長男長女・中間子・末っ子・一人っ子の性格の傾向
同じ環境で生まれ育っても、生まれ順で性格は違うものなのだろうか。家庭教育研究家の田宮由美さんに教えてもらった。
-
mallocについて
C言語・C++・C#
-
ブラウザ自体の事じゃなくてJavaScriptで書かれたブラウザ上で動作するアプリのことをなんといい
JavaScript
-
パイソンの
C言語・C++・C#
-
-
4
コードを書いて下さい( ; ; )写真有り
HTML・CSS
-
5
C言語をコンパイルするとコンピュータ上でどう動くかというコードになるみたいな説明をよく聞くのですがこ
C言語・C++・C#
-
6
プログラミング
その他(プログラミング・Web制作)
-
7
プログラミング ソースコード
その他(プログラミング・Web制作)
-
8
プログラミングについての質問です。 #include <stdio.h> #include <std
C言語・C++・C#
-
9
関数定義について質問です 架空のプログラミング言語ですが、こういう関数があった時、何を返してくれそう
その他(プログラミング・Web制作)
-
10
pythonの質問です。
C言語・C++・C#
-
11
ものづくりに向いているプログラミング言語ってなんですか?家電にもプログラミングが使われてるとか
その他(プログラミング・Web制作)
-
12
double型が正常に認識されていない恐れ
C言語・C++・C#
-
13
プログラミング、アーキテクチャ好きに質問です。 kotlinなどのMVVM、単方向データフローの考え
その他(プログラミング・Web制作)
-
14
Excel-VBAのmsgBox()の不思議
Visual Basic(VBA)
-
15
[C言語]fputsとfprintfの違い
C言語・C++・C#
-
16
テキストファイルのファイル名を抽出してBAT処理する方法はありますか。
その他(プログラミング・Web制作)
-
17
どちのほうがすきですか?
C言語・C++・C#
-
18
こんなことてしますか??
C言語・C++・C#
-
19
楽しくて最高のプログラミング言語を作りたいのですが、そもそもプログラミング言語を作る意味なんてないと
その他(プログラミング・Web制作)
-
20
ジャバスクリプトについて。
JavaScript
関連するカテゴリからQ&Aを探す
おすすめ情報
このQ&Aを見た人がよく見るQ&A
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
三項でたとえば交換って
-
Excel VBAでsub,dimは何の略?
-
VB6で配列を引数にするときの2...
-
VBAでcallで呼び出したsubを終...
-
アクセスできない保護レベルエ...
-
sublimit textっていうエディタ...
-
C言語のサフィックスについて
-
プログラムの素朴な質問です 分...
-
C#のループでtextboxに値を入れ...
-
【HTML、VBScript】HTAアプリケ...
-
Excel ユーザーフォームで計算 ...
-
VBAで入力数値について
-
ボールが壁に当たって跳ね返る...
-
VS CodeでTEXファイルにPDF形式...
-
Verilogの文法
-
GetNextWindowがDLLファイルUse...
-
texで図と表を並べたい
-
VB6でClickイベントを一時的に...
-
【VB.NET】テキストボックスに...
-
TextBoxの内容を右寄せ
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
三項でたとえば交換って
-
Functionの戻り値を配列にした...
-
Excel VBAでsub,dimは何の略?
-
xmlの値を配列変数に格納し...
-
VB6で配列を引数にするときの2...
-
配列に複数の値があるか簡潔に...
-
2次元配列のデータをファイルへ...
-
xmlの値を配列に格納したいんで...
-
ExcelVBAで他のファイル(proje...
-
VBA public変数はどのようなこ...
-
C#のループでtextboxに値を入れ...
-
VBAでcallで呼び出したsubを終...
-
他のフォームから別のフォーム...
-
C言語のサフィックスについて
-
レコードセットにnullの場合
-
エクセルVBAでテキストボッ...
-
プログラムの素朴な質問です 分...
-
VB.NETでのイベントの途中終了
-
sublimit textっていうエディタ...
-
アクセスできない保護レベルエ...
おすすめ情報