Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. sum([],0).
  2. sum([X|Xs],Total):- sum(Xs,Rest) , Total is X+Rest.
  3.  
  4.  
  5.  
  6. add_N_spaces(Line, Line,0).
  7. add_N_spaces([' '|Line],RestLine,N) :-N>0, N1 is N-1,
  8. add_N_spaces(Line, RestLine,N1).
  9.  
  10.  
  11. add_N_stars(Line, Line,0).
  12. add_N_stars([*|Line],RestLine,N) :- N>0,N1 is N-1,
  13. add_N_stars(Line, RestLine,N1).
  14.  
  15.  
  16.  
  17. vecLine2BitLine(X,[Y|Ys],N,BitLine):-
  18. LastSpace is Y-1,
  19. last(X,LastStarLoc),
  20. last([Y|Ys],LastStarLen),
  21. vecLine2BitLine_aux(X,[Y|Ys],N,Tmp,LastSpace),
  22. LastSpaces = N - LastStarLoc - LastStarLen + 1,
  23. add_N_spaces(ListOfSpaces,[],LastSpaces),
  24. append(Tmp,ListOfSpaces,BitLine).
  25.  
  26. vecLine2BitLine_aux([],[],_,[],_).
  27. vecLine2BitLine_aux([X|Xs],[Y|Ys],N,BitLine,LastSpace):-
  28. NumOfSpaces is Y-LastSpace ,
  29.  
  30. add_N_spaces(ListOfSpaces,[],NumOfSpaces),
  31. add_N_stars(ListOfStars,[],X),
  32. append(ListOfSpaces,ListOfStars,Tmp),
  33. length(Tmp,Size),
  34. NewN is N-Size,
  35. LastSpace2 is X+Y,
  36. vecLine2BitLine_aux(Xs,Ys,NewN,Tmp2,LastSpace2),
  37. append(Tmp,Tmp2,BitLine).
  38.  
  39.  
  40. insertBegining([Element|List],List,Element).
  41.  
  42.  
  43.  
  44.  
  45. makeListc([],_,[]).
  46. makeListc([M|Rest],Index,List):-
  47. nth1(Index,M,Element),
  48. insertBegining(AlmostList,[],Element),
  49. makeListc(Rest,Index,Temp),
  50. append(AlmostList,Temp,List).
  51.  
  52. trans(Matrix,TransposedMatrix):-
  53.  
  54. length(Matrix,Len),
  55. NLen is Len+1,
  56. trans_aux(Matrix,1,TransposedMatrix,NLen).
  57.  
  58.  
  59. trans_aux(_,Len,[],Len).
  60. trans_aux([M|Rest],Index,NewMatrix,Len):-
  61. makeListc([M|Rest],Index,Listi),
  62. IndexNew is Index + 1,
  63. trans_aux([M|Rest],IndexNew,Almost,Len),
  64. insertBegining(NewMatrix,Almost,Listi).
  65.  
  66. trim(L,N,S) :- % to trim N elements from a list
  67. append(P,S,L) , % - split L into a prefix and suffix
  68. length(P,N) .
  69.  
  70. starsInArow([' '|_],0).
  71. starsInArow([' '],0).
  72. starsInArow([],0).
  73. starsInArow([*|RestBit],N):-
  74. starsInArow(RestBit,C),
  75. N is C + 1.
  76.  
  77.  
  78. isBitLineLegal([],[]).
  79. isBitLineLegal([' '|[]],[]).
  80. isBitLineLegal([' '|RestBit],[LenLine|RestLine]):-
  81. isBitLineLegal(RestBit,[LenLine|RestLine]).
  82.  
  83. isBitLineLegal([*|RestBit],[LenLine|RestLine]):-
  84. starsInArow([*|RestBit],N),
  85. N =:= LenLine ,
  86. trim([*|RestBit],LenLine,S),
  87. isBitLineLegal(S,RestLine).
  88.  
  89.  
  90. vecLineAll2BitLineAll([],[],_,[]).
  91. vecLineAll2BitLineAll([LenLineAll|RestL],[PosLineAll|RestP],N,BitLineAll):-
  92. vecLine2BitLine(LenLineAll,PosLineAll,N,Temp),
  93. vecLineAll2BitLineAll(RestL,RestP,N,Rest),
  94. insertBegining(BitLineAll,Rest,Temp).
  95.  
  96.  
  97. isBitLineLegalAll([],[]).
  98. isBitLineLegalAll([BitLineAll|RestBit],[LenLineAll|RestL]):-
  99. isBitLineLegal(BitLineAll,LenLineAll),
  100. isBitLineLegalAll(RestBit,RestL).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement