2014-12-05 3 views
1

슬라이드라는 html 파일이 포함 된 디렉터리 트리가 있습니다. 예 :목록이 들어있는 두 개의 개체 목록을 병합합니다.

slides_root 
| 
|_slide-1 
| |_slide-1.html 
| |_slide-2.html 
| 
|_slide-2 
| | 
| |_slide-1 
| | |_slide-1.html 
| | |_slide-2.html 
| | |_slide-3.html 
| | 
| |_slide-2 
| |_slide-1.html 

... 등등. 그들은 더 깊게 갈 수 있습니다. 이제이 구조의 일부 슬라이드를이 하위 집합 인 다른 트리와 병합하여 교체해야한다고 상상해보십시오. 예제를 통해

: 나는 내부 슬라이드 1.html 및 슬라이드 3.html을 대체 할 말 "slides_root/슬라이드-2/슬라이드-1"과 "slides_root을"병합 :

slide_to_change 
| 
|_slide-2 
    | 
    |_slide-1 
    |_slide-1.html 
    |_slide-3.html 

"slides_to_change"를 "slides_root"에 병합합니다. 구조는 동일하므로 모든 것이 잘됩니다. 하지만이 계획의 비단뱀 객체 표현에서해야합니다.

Slide(object): 

def __init__(self, path): 
    self.path = path 
    self.slides = [Slide(path)] 

모두 슬라이드 1과 슬라이드 2는 경로 포함 목록이 포함되어 있습니다 : - slides1, slides2 - 그래서 두 그루의 나무는 두 개의 인스턴스로 표현됩니다

의 구조는 동일한 "슬라이드"클래스의 다음 다른 슬라이드 개체와 다른 경로 및 슬라이드 개체 목록 등이 있습니다.

상대 경로가 동일하면 slide1의 슬라이드 개체를 slide2의 슬라이드 개체로 바꿉니다.

이 결과를 얻으려면 어떻게해야합니까? 정말 어려워서 탈출구를 볼 수 없습니다. 다음과 같이 이상적으로 표시됩니다.

for slide_root in slide1.slides: 
    for slide_dest in slide2.slides: 
     if slide_root.path == slide_dest.path: 
      slide_root = slide_dest 
// now restart the loop at a deeper level 
// repeat 

답해 주셔서 감사합니다.

답변

1

소리가 그렇게 복잡하지 않습니다.

삽입 트리를 걷는 데 재귀 함수를 사용하고 오래된 트리의 해당 위치를 유지하십시오.

If the parts match: 
    If the parts are both leafs (html thingies): 
     Insert (overwrite) the value. 
    If the parts are both nodes (slides): 
     Call yourself with the subslides (here's the recursion). 

나는 이것이 단지 힌트 일 뿐이라는 것을 알고있다. 하지만 어쩌면이 일을 시작하고 싶을 수도 있습니다.

def merge_slide(slide, old_slide): 
    for sub_slide in slide.slides: 
    sub_slide_position_in_old_slide = find_sub_slide_position_by_path(sub_slide.path) 
    if sub_slide_position_in_old_slide >= 0: # we found a match! 
     sub_slide_in_old_slide = old_slide.slides[sub_slide_position_in_old_slide] 
     if sub_slide.slides: # this is a node! 
     merge_slide(sub_slide, sub_slide_in_old_slide) # here we recurse 
     else: # this is a leaf! so we replace it: 
     old_slide[sub_slide_position_in_old_slide] = sub_slide 
    else: # nothing like this in old_slide 
     pass # ignore (you might want to consider this case!) 

은 어쩌면 당신에게 내가이 접근 할 방법에 대한 아이디어를 제공합니다 : 파이썬에서는 이런 STH (도 완전히 구체화되지 않음)를 볼 수 있었다.

관련 문제