ex01: fix: span understood
This commit is contained in:
		@ -1,4 +1,5 @@
 | 
			
		||||
#include "Span.hpp"
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <bits/stdc++.h>
 | 
			
		||||
 | 
			
		||||
Span::Span()
 | 
			
		||||
@ -27,16 +28,22 @@ Span& Span::operator=(const Span& src)
 | 
			
		||||
 | 
			
		||||
int Span::shortestSpan() const
 | 
			
		||||
{
 | 
			
		||||
    if (this->_vector.size() == 0)
 | 
			
		||||
    if (this->_vector.size() < 2)
 | 
			
		||||
        throw std::exception();
 | 
			
		||||
    return *std::min_element(this->_vector.begin(), this->_vector.end());
 | 
			
		||||
    std::vector<int> tmp = this->_vector;
 | 
			
		||||
    std::sort(tmp.begin(), tmp.end());
 | 
			
		||||
    int min_range = tmp[1] - tmp[0];
 | 
			
		||||
    for (unsigned int i = 1; i + 1 != tmp.size(); i++)
 | 
			
		||||
        if (min_range > tmp[i + 1] - tmp[i])
 | 
			
		||||
            min_range = tmp[i + 1] - tmp[i];
 | 
			
		||||
    return min_range;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int Span::longestSpan() const
 | 
			
		||||
{
 | 
			
		||||
    if (this->_vector.size() == 0)
 | 
			
		||||
        throw std::exception();
 | 
			
		||||
    return *std::max_element(this->_vector.begin(), this->_vector.end());
 | 
			
		||||
    return *std::max_element(this->_vector.begin(), this->_vector.end()) - *std::min_element(this->_vector.begin(), this->_vector.end());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Span::addNumber(const int nb)
 | 
			
		||||
 | 
			
		||||
@ -53,7 +53,7 @@ int main()
 | 
			
		||||
        span.addNumber(2);
 | 
			
		||||
        span.addNumber(3);
 | 
			
		||||
        span.addNumber(4);
 | 
			
		||||
        if (span.shortestSpan() == 0)
 | 
			
		||||
        if (span.shortestSpan() == 1)
 | 
			
		||||
            std::cout << "ok" << std::endl;
 | 
			
		||||
        else
 | 
			
		||||
            std::cout << "failed !" << std::endl;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user