Diff
checker
テキスト
テキスト
画像
ドキュメント
Excel
フォルダ
Legal
Enterprise
デスクトップ
料金
ログイン
Diffchecker デスクトップのダウンロード
テキスト比較
2 つのテキスト ファイルの違いを見つける
ツール
履歴
ライブエディター
未変更行を折りたたむ
折り返しなし
レイアウト
分割
統合
比較精度
スマート
単語
文字
シンタックスハイライト
構文を選択
無視
テキスト変換
最初の差分へ移動
入力を編集
Diffchecker Desktop
Diffcheckerを実行する最も安全な方法。Diffchecker Desktopアプリを入手:あなたの差分はコンピューターから出ることはありません!
Desktopを入手
Solution to hwk7prob2
作成日
5 年前
差分は期限切れになりません
クリア
エクスポート
共有
説明
11 削除
行
合計
削除
文字
合計
削除
この機能を引き続き使用するには、アップグレードしてください
Diff
checker
Pro
価格を見る
50 行
すべてコピー
33 追加
行
合計
追加
文字
合計
追加
この機能を引き続き使用するには、アップグレードしてください
Diff
checker
Pro
価格を見る
63 行
すべてコピー
"""Detect matching of parentheses in an expression"""
"""Detect matching of parentheses in an expression"""
# MCS 260 Fall 2021 Lecture 17
# MCS 260 Fall 2021 Lecture 17
コピー
コピー済み
コピー
コピー済み
# Read expression (which should include parentheses
)
# Read expression (which should include parentheses
/brackets
)
s = input("Expression: ")
s = input("Expression: ")
コピー
コピー済み
コピー
コピー済み
# We'll use a stack to keep track of all the "(
"
# We'll use a stack to keep track of all the "(
" or "[
"
# that haven't been matched with ")"
yet. Every
# that haven't been matched with ")"
or "]"
yet. Every
# new
"("
we see gets pushed, and every
")"
we see
# new
opening delimiter
we see gets pushed, and every
closing
#
closes whatever is at the top of the stack.
# delimiter
we see
closes whatever is at the top of the stack.
currently_open = []
currently_open = []
# We want both the characters of s and their positions
# We want both the characters of s and their positions
# so we use enumerate()
# so we use enumerate()
for i,c in enumerate(s):
for i,c in enumerate(s):
# c is character from s
# c is character from s
# i is the position (0-based index) of that character in s
# i is the position (0-based index) of that character in s
コピー
コピー済み
コピー
コピー済み
if c == "(
":
if c == "(
" or c == "[
":
# New left paren
opened; push it
# New left paren
/bracket
opened; push it
currently_open.append(
i
)
currently_open.append(
[c,i]
)
elif c == ")
":
elif c == ")
" or c == "]
":
# Right
paren
closes whatever left
paren
is at the
# Right
delim
closes whatever left
delim
is at the
# top of the stack. But we need to make sure the
# top of the stack. But we need to make sure the
# stack is nonempty before trying to pop.
# stack is nonempty before trying to pop.
try:
try:
コピー
コピー済み
コピー
コピー済み
currently_open.pop()
# i0 and c0 are the corresponding i and c
# for the opening paren/bracket
c0, i0 =
currently_open.pop()
if (c0 == "(" and c == "]") or (c0=="[" and c == ")"):
print("Error:")
print(s)
print(" "*i0 + "^ has mismatched delimiter types")
print("First delimiter is " + c0)
print("Second delimiter is " + c)
exit()
else:
print("Matching delimiters found: " + s[i0:i+1])
except IndexError:
except IndexError:
コピー
コピー済み
コピー
コピー済み
# Error because there was no
"("
on the
# Error because there was no
opening delim
on the
# stack to match
this ")"
# stack to match
the closing delimiter
print("Error:")
print("Error:")
print(s)
print(s)
print(" "*i + "^ does not match any preceding (")
print(" "*i + "^ does not match any preceding (")
exit()
exit()
コピー
コピー済み
コピー
コピー済み
# are there any
parentheses
open?
# are there any
delimiters
open?
# If so, it means that there is a (
with no match
# If so, it means that there is a (
or [
with no match
if len(currently_open) > 0:
if len(currently_open) > 0:
print("Error:")
print("Error:")
print(s)
print(s)
print(" "*currently_open.pop() + "^ is not matched by any following )")
print(" "*currently_open.pop() + "^ is not matched by any following )")
else:
else:
コピー
コピー済み
コピー
コピー済み
print("
Parentheses
matched successfully.")
print("
Delimiters
matched successfully.")
# Examples of what we expect the error messages to look like:
# Examples of what we expect the error messages to look like:
# (1 + ((2+3) - 5
# (1 + ((2+3) - 5
# ^ is not matched by any following )
# ^ is not matched by any following )
# ( 1 + (3-4))) + 5
# ( 1 + (3-4))) + 5
# ^ does not match any preceding (
# ^ does not match any preceding (
保存された差分
原文
ファイルを開く
"""Detect matching of parentheses in an expression""" # MCS 260 Fall 2021 Lecture 17 # Read expression (which should include parentheses) s = input("Expression: ") # We'll use a stack to keep track of all the "(" # that haven't been matched with ")" yet. Every # new "(" we see gets pushed, and every ")" we see # closes whatever is at the top of the stack. currently_open = [] # We want both the characters of s and their positions # so we use enumerate() for i,c in enumerate(s): # c is character from s # i is the position (0-based index) of that character in s if c == "(": # New left paren opened; push it currently_open.append(i) elif c == ")": # Right paren closes whatever left paren is at the # top of the stack. But we need to make sure the # stack is nonempty before trying to pop. try: currently_open.pop() except IndexError: # Error because there was no "(" on the # stack to match this ")" print("Error:") print(s) print(" "*i + "^ does not match any preceding (") exit() # are there any parentheses open? # If so, it means that there is a ( with no match if len(currently_open) > 0: print("Error:") print(s) print(" "*currently_open.pop() + "^ is not matched by any following )") else: print("Parentheses matched successfully.") # Examples of what we expect the error messages to look like: # (1 + ((2+3) - 5 # ^ is not matched by any following ) # ( 1 + (3-4))) + 5 # ^ does not match any preceding (
変更されたテキスト
ファイルを開く
"""Detect matching of parentheses in an expression""" # MCS 260 Fall 2021 Lecture 17 # Read expression (which should include parentheses/brackets) s = input("Expression: ") # We'll use a stack to keep track of all the "(" or "[" # that haven't been matched with ")" or "]" yet. Every # new opening delimiter we see gets pushed, and every closing # delimiter we see closes whatever is at the top of the stack. currently_open = [] # We want both the characters of s and their positions # so we use enumerate() for i,c in enumerate(s): # c is character from s # i is the position (0-based index) of that character in s if c == "(" or c == "[": # New left paren/bracket opened; push it currently_open.append([c,i]) elif c == ")" or c == "]": # Right delim closes whatever left delim is at the # top of the stack. But we need to make sure the # stack is nonempty before trying to pop. try: # i0 and c0 are the corresponding i and c # for the opening paren/bracket c0, i0 = currently_open.pop() if (c0 == "(" and c == "]") or (c0=="[" and c == ")"): print("Error:") print(s) print(" "*i0 + "^ has mismatched delimiter types") print("First delimiter is " + c0) print("Second delimiter is " + c) exit() else: print("Matching delimiters found: " + s[i0:i+1]) except IndexError: # Error because there was no opening delim on the # stack to match the closing delimiter print("Error:") print(s) print(" "*i + "^ does not match any preceding (") exit() # are there any delimiters open? # If so, it means that there is a ( or [ with no match if len(currently_open) > 0: print("Error:") print(s) print(" "*currently_open.pop() + "^ is not matched by any following )") else: print("Delimiters matched successfully.") # Examples of what we expect the error messages to look like: # (1 + ((2+3) - 5 # ^ is not matched by any following ) # ( 1 + (3-4))) + 5 # ^ does not match any preceding (
違いを見つける