lab3b
This commit is contained in:
parent
a0ef3f0818
commit
cec08d1f03
|
@ -0,0 +1,8 @@
|
||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="jdk" jdkName="C:\Users\Gao42H\miniconda3" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="Python 3.12" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="C:\Users\Gao42H\miniconda3" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/SELab1.iml" filepath="$PROJECT_DIR$/.idea/SELab1.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="PySciProjectComponent">
|
||||||
|
<option name="PY_INTERACTIVE_PLOTS_SUGGESTED" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1 @@
|
||||||
|
The sunlight filtered through the dense canopy, casting intricate patterns on the forest floor. Birds chirped melodiously, creating a symphony of nature finest sounds. Amidst the greenery, a small stream gurgled, its waters crystal clear and refreshing. Suddenly, a deer darted past, startling.To @ explore strange new worlds and worlds and seek strange,To seek out new life and new worlds and new civilizations?
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
27
oop.py
27
oop.py
|
@ -270,20 +270,34 @@ class Tu:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
def calcShortestPath(self, word1, word2):
|
def calcShortestPath(self, word1, word2):
|
||||||
|
if word1 not in self.graph or word2 not in self.graph:
|
||||||
|
print("No", word1, "or", word2, "in the graph!")
|
||||||
|
return [],0
|
||||||
|
|
||||||
if word2 == "":
|
if word2 == "":
|
||||||
word1 = self.input_check(word1)
|
word1 = self.input_check(word1)
|
||||||
for wordtmp in self.dict:
|
for wordtmp in self.dict:
|
||||||
if wordtmp == word1:
|
if wordtmp == word1:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
self.calc_shortest_path(word1, wordtmp)
|
result=self.calc_shortest_path(word1, wordtmp)
|
||||||
|
if result == []:
|
||||||
|
print("No path exists between", word1, "and", word2)
|
||||||
|
else:
|
||||||
|
print(result)
|
||||||
|
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
word1 = self.input_check(word1)
|
word1 = self.input_check(word1)
|
||||||
word2 = self.input_check(word2)
|
word2 = self.input_check(word2)
|
||||||
self.calc_shortest_path(word1, word2)
|
result = self.calc_shortest_path(word1, word2)
|
||||||
pass
|
pass
|
||||||
|
if result==[] :
|
||||||
|
print("No path exists between", word1, "and", word2)
|
||||||
|
else:
|
||||||
|
print(result)
|
||||||
|
result_len = 0 if result==[] or result is None else len(result[0])-1
|
||||||
|
return result,result_len
|
||||||
|
|
||||||
def calc_shortest_path_old(self, word1, word2):
|
def calc_shortest_path_old(self, word1, word2):
|
||||||
|
|
||||||
|
@ -298,8 +312,10 @@ class Tu:
|
||||||
shortest_path_list.append(shortest_path)
|
shortest_path_list.append(shortest_path)
|
||||||
if shortest_path_list.__len__() > 0:
|
if shortest_path_list.__len__() > 0:
|
||||||
self.draw_shortest_path(shortest_path_list)
|
self.draw_shortest_path(shortest_path_list)
|
||||||
|
return shortest_path_list
|
||||||
else:
|
else:
|
||||||
print("No path exists between", word1, "and", word2)
|
return []
|
||||||
|
#print("No path exists between", word1, "and", word2)
|
||||||
|
|
||||||
def calc_shortest_path(self, word1, word2):
|
def calc_shortest_path(self, word1, word2):
|
||||||
|
|
||||||
|
@ -313,8 +329,11 @@ class Tu:
|
||||||
|
|
||||||
if shortest_path_list.__len__() > 0:
|
if shortest_path_list.__len__() > 0:
|
||||||
self.draw_shortest_path(shortest_path_list)
|
self.draw_shortest_path(shortest_path_list)
|
||||||
|
return shortest_path_list
|
||||||
else:
|
else:
|
||||||
print("No path exists between", word1, "and", word2)
|
return []
|
||||||
|
|
||||||
|
#print("No path exists between", word1, "and", word2)
|
||||||
|
|
||||||
def random_traversal(self):
|
def random_traversal(self):
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
melodiously
|
||||||
|
creating
|
||||||
|
a
|
||||||
|
small
|
||||||
|
stream
|
||||||
|
gurgled
|
||||||
|
its
|
||||||
|
waters
|
||||||
|
crystal
|
||||||
|
clear
|
||||||
|
and
|
||||||
|
refreshing
|
||||||
|
suddenly
|
||||||
|
a
|
||||||
|
deer
|
||||||
|
darted
|
||||||
|
past
|
||||||
|
startling
|
||||||
|
to
|
||||||
|
seek
|
||||||
|
out
|
||||||
|
new
|
||||||
|
worlds
|
||||||
|
and
|
||||||
|
new
|
||||||
|
life
|
||||||
|
and
|
||||||
|
worlds
|
|
@ -0,0 +1,34 @@
|
||||||
|
and
|
||||||
|
seek
|
||||||
|
out
|
||||||
|
new
|
||||||
|
life
|
||||||
|
and
|
||||||
|
refreshing
|
||||||
|
suddenly
|
||||||
|
a
|
||||||
|
symphony
|
||||||
|
of
|
||||||
|
nature
|
||||||
|
finest
|
||||||
|
sounds
|
||||||
|
amidst
|
||||||
|
the
|
||||||
|
forest
|
||||||
|
floor
|
||||||
|
birds
|
||||||
|
chirped
|
||||||
|
melodiously
|
||||||
|
creating
|
||||||
|
a
|
||||||
|
deer
|
||||||
|
darted
|
||||||
|
past
|
||||||
|
startling
|
||||||
|
to
|
||||||
|
explore
|
||||||
|
strange
|
||||||
|
new
|
||||||
|
worlds
|
||||||
|
and
|
||||||
|
worlds
|
|
@ -0,0 +1,24 @@
|
||||||
|
nature
|
||||||
|
finest
|
||||||
|
sounds
|
||||||
|
amidst
|
||||||
|
the
|
||||||
|
greenery
|
||||||
|
a
|
||||||
|
deer
|
||||||
|
darted
|
||||||
|
past
|
||||||
|
startling
|
||||||
|
to
|
||||||
|
seek
|
||||||
|
strange
|
||||||
|
new
|
||||||
|
life
|
||||||
|
and
|
||||||
|
seek
|
||||||
|
out
|
||||||
|
new
|
||||||
|
worlds
|
||||||
|
and
|
||||||
|
new
|
||||||
|
civilizations
|
76
test.py
76
test.py
|
@ -5,21 +5,78 @@ class TextProcessor(Tu):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestQueryBridgeWords(unittest.TestCase):
|
class TestQueryBridgeWords(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# Initialize a graph for testing
|
# Initialize a graph for testing
|
||||||
self.graph = {
|
self.graph = {
|
||||||
'a': {'b': 1, 'c': 2},
|
'a': {'b': 1, 'c': 2},
|
||||||
'b': {'c': 1, 'd': 2},
|
'b': {'c': 1, 'd': 2},
|
||||||
'c': {'d': 1},
|
'c': {'d': 1},
|
||||||
'x': {},
|
'x': {},
|
||||||
'z': {}
|
'z': {}
|
||||||
}
|
}
|
||||||
self.processor = TextProcessor()
|
self.processor = TextProcessor()
|
||||||
self.processor.generate_directed_dict(self.processor.read_text_file("1.txt"))
|
self.processor.generate_directed_dict(self.processor.read_text_file("1.txt"))
|
||||||
self.processor.generate_directed_graph()
|
self.processor.generate_directed_graph()
|
||||||
|
def test_normal_path1(self):
|
||||||
|
# 正常情况:存在路径
|
||||||
|
word1 = 'to'
|
||||||
|
word2 = 'out'
|
||||||
|
expected_path = ['to', 'seek', 'out']
|
||||||
|
expected_length = 2
|
||||||
|
path,length = self.processor.calcShortestPath(word1, word2)
|
||||||
|
|
||||||
|
self.assertEqual(path, [expected_path])
|
||||||
|
self.assertEqual(length, expected_length)
|
||||||
|
|
||||||
|
def test_normal_path2(self):
|
||||||
|
# 正常情况:存在路径
|
||||||
|
word1 = 'to'
|
||||||
|
word2 = 'new'
|
||||||
|
expected_path = ['to', 'seek', 'out',"new"],['to', 'seek', "strange",'new'],['to', 'explore', "strange",'new']
|
||||||
|
expected_length = 3
|
||||||
|
path, length = self.processor.calcShortestPath(word1, word2)
|
||||||
|
|
||||||
|
self.assertEqual(path, [expected_path])
|
||||||
|
self.assertEqual(length, expected_length)
|
||||||
|
|
||||||
|
def test_no_path(self):
|
||||||
|
# 无路径情况
|
||||||
|
word1 = 'civilizations'
|
||||||
|
word2 = 'to'
|
||||||
|
expected_path = []
|
||||||
|
expected_length = 0
|
||||||
|
|
||||||
|
path, length = self.processor.calcShortestPath(word1, word2)
|
||||||
|
self.assertEqual(path, expected_path)
|
||||||
|
self.assertEqual(length, expected_length)
|
||||||
|
|
||||||
|
def test_node_not_exists(self):
|
||||||
|
# 节点不存在情况
|
||||||
|
word1 = 'now'
|
||||||
|
word2 = 'lod'
|
||||||
|
expected_path = []
|
||||||
|
expected_length = 0
|
||||||
|
|
||||||
|
path, length = self.processor.calcShortestPath(word1, word2)
|
||||||
|
self.assertEqual(path, expected_path)
|
||||||
|
self.assertEqual(length, expected_length)
|
||||||
|
|
||||||
|
def test_same_node(self):
|
||||||
|
# 同一节点情况
|
||||||
|
word1 = 'to'
|
||||||
|
word2 = 'to'
|
||||||
|
expected_path = []
|
||||||
|
expected_length = 0
|
||||||
|
|
||||||
|
path, length = self.processor.calcShortestPath(word1, word2)
|
||||||
|
self.assertEqual(path, expected_path)
|
||||||
|
self.assertEqual(length, expected_length)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_valid_bridge_words_exist(self):
|
def test_valid_bridge_words_exist(self):
|
||||||
# Valid equivalence class: bridge words exist
|
# Valid equivalence class: bridge words exist
|
||||||
|
@ -43,7 +100,7 @@ class TestQueryBridgeWords(unittest.TestCase):
|
||||||
|
|
||||||
def test_invalid_both_words_not_in_graph(self):
|
def test_invalid_both_words_not_in_graph(self):
|
||||||
# Invalid equivalence class: both words not in graph
|
# Invalid equivalence class: both words not in graph
|
||||||
result = self.processor.queryBridgeWords('an', 'sought')
|
result = self.processor.queryBridgeWords('now', 'lod')
|
||||||
self.assertEqual(result, [])
|
self.assertEqual(result, [])
|
||||||
|
|
||||||
def test_edge_case_min_word(self):
|
def test_edge_case_min_word(self):
|
||||||
|
@ -66,5 +123,6 @@ class TestQueryBridgeWords(unittest.TestCase):
|
||||||
result = self.processor.queryBridgeWords('the', 'floor')
|
result = self.processor.queryBridgeWords('the', 'floor')
|
||||||
self.assertIn('forest', result)
|
self.assertIn('forest', result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue