code.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* Write a function to create a Christmas tree based on height h.
  2. * tree(0) ➞ []
  3. * tree(1) ➞
  4. "#"
  5. tree(2) ➞
  6. " # ",
  7. "###"
  8. tree(5) ➞
  9. " # ",
  10. " ### ",
  11. " ##### ",
  12. " ####### ",
  13. "#########"
  14. // 0000 5 0000
  15. // 000 555 000
  16. // 00 55555 00
  17. // 0 5555555 0
  18. // 555555555
  19. // the "whorls" [each level of the tree] are odd in length.
  20. // the integer passed to the function will create
  21. // "h" amount of strings.
  22. // the length of each whorl is "h" +"h-1"
  23. // airSpace ("air") around the branches is "air"
  24. std::vector<std::string> treeX(int h) {
  25. std::vector<std::string> whorl;
  26. int maxWhorl = h + (h-1);
  27. for(;h>0;h--){
  28. c = "#";
  29. // create the base of the tree & maxWhorl
  30. }
  31. }
  32. */
  33. std::vector<std::string> tree(int h) {
  34. std::vector<std::string> r;
  35. // set the base whorl (abs protects against 0 height tree)
  36. std::string base = std::string(abs(h+h-1),'#');
  37. for(int i=0;i<h;i++){
  38. // prepend previous whorl to tree
  39. r.insert(r.begin(),base);
  40. // reduce size of whorl for next loop
  41. base[base.size()-i-1] = ' ';
  42. base[i] = ' ';
  43. }
  44. return r;
  45. }